Functional tools in Go 1.18 using newly introduced generics

Related tags

Utilities functools



functools is a simple Go library that brings you your favourite functional paradigms without sacrificing type-safety using interface{} or reflect

Made possible by Go 1.18 using the newly introduced generics.


  • Any
  • All
  • Count
  • Filter
  • ForEach
  • Map
  • Reduce
  • Sum


go get -u


import (

type User struct {
	username     string
	hasPortfolio bool

var users = []User{
		{"gopher", true},
		{"rakeeb", false},
		{"jack", true}}

func main() {
    // Count users with linked portfolios
    fmt.Printf("num users with linked portfolios: %d", 
        functools.Count(users, func(u User) bool { return u.hasPortfolio }))

    // Print usernames of users with linked portfolios
        functools.Filter(users, func(u User) bool { return u.hasPortfolio }),
        func(u User) { fmt.Printf("%s has a linked portfolio\n", u.username) })

Documentation does not yet support Go 1.18 packages that use generics:

For now, documentation is provided via comments and by running go doc -all from the package directory.



  • `Chunk` func

    `Chunk` func

    Chunk consumes a slice of a generic type and returns a slice composed of multiple chunks of a user-specified size

    Vacuously, empty slices return empty regardless of the chunk size.

    When the chunk size is zero or negative, return an empty slice


    slice := []int{1, 2, 3, 4}
    chunk := Chunk(slice, 2) // []int{{1,2}, {3, 4}}
    chunk = Chunk(slice, 3) // []int{{1, 2, 3}, {4}}
    chunk = Chunk(slice, 10) // []int{{1, 2, 3, 4}}
    chunk = Chunk(slice, 0) // []int{}
    opened by qianxi0410 3
  • Filter: redundant memory allocation

    Filter: redundant memory allocation

    You should not allocate new slice, it can be too expensive, just reuse old one

    func Filter[T any](slice []T, predicate func(T) bool) []T {
    	res := slice[:0]
    	for _, v := range slice {
    		if predicate(v) {
    			res = append(res, v)
    	return res
    opened by crayoned 2
  • ReduceRight


    I think ReduceRight is a good addition for completeness sake.

    Sometimes the order of evaluation for a reduce changes the result.


    strConcat := func(a, b string) string { return a + b }
    slice := []string{"a", "b", "c"}
    Reduce(slice, "", strConcat) // Output: "abc"
    ReduceRight(slice, "", strConcat) // Output: "cba"
    opened by nchengyeeshen 0
Rakeeb Hossain
CS student @uWaterloo
Rakeeb Hossain
Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Walter Schulze 998 Aug 5, 2022
Experimenting with golang generics to implement functional favorites like filter, map, && reduce.

funcy Experimenting with golang generics to implement functional favorites like filter, map, && reduce. 2021-12 To run the tests, you need to install

null 0 Dec 29, 2021
A collection of functional operators for golang with generics

fn fn is a collection of go functional operators with generics Getting Started P

Billy Irwin 9 Jul 8, 2022
F - Experimenting with Go 1.18 generics to write more functional Go code

f f is a simple library that leverages the new generics in Golang to create a tools for functional style of code. Pipe like '|>' in Elixir or Elm. inp

Amirreza Askarpour 3 Apr 12, 2022
Extended library functions using generics in Go.

Just few extended standard library functions for Golang using generics.

null 1 Dec 16, 2021
Optional type using Go 1.18 generics. Generic Optional (or Go Optional, if you prefer) goptional is a package that provides an implementation of an Optional[T] monad

Ethan P. 0 Apr 2, 2022
Use is a go utility library using go1.18 generics

use use is a go utility library using go1.18 generics created by halpdesk 2022-01-22 use/slice Map updates a slice by applying a function to all membe

halpdesk 0 Jan 22, 2022
Helper functions for common scenarios, using Go generics.

zeroflucs generics When writing Go code for Go 1.17 or below, we've all written more than our fair share of methods to check "does this slice contain

ZeroFlucs 2 Feb 18, 2022
Higher Order Functions using Golang Generics (Hack Days 2022)

hoff: Higher Order Functions (and Friends) Golang 1.18+ implementations of common methods/data structures using Go Generics Requirements Go 1.18 or ne

Shopify 16 Jul 29, 2022
Helpfully Functional Go like underscore.js

/\ \ __ __ ___ \_\ \ __ _ __ ____ ___ ___ _ __ __ __ __

null 376 Aug 8, 2022
Make Go functional with dogs

dogs Make Go functional with dogs Caution This is a highly-experimental package. Any changes will be made in a backward-incompatible manner. This pack

Genta Kamitani 36 May 24, 2022
Utilities and immutable collections for functional programming in Golang

Utilities and immutable collections for functional programming in Golang. This is an experimental library to play with the new Generics Feature in Go 1.18.

Peter Zeller 8 May 20, 2022
Experiments with Go generics

generics Quick experiments with Go generics algebra, a generic square root function for float, complex and and rational. future, a concurrent cache ("

Alan Donovan 80 Aug 5, 2022
Example code for Go generics

go-generics-example Example code for Go generics. Usage $ go build -gcflags=-G=3 Requirements Go 1.17 or later Advertise Go 言語にやってくる Generics は我々に何をも

mattn 1.2k Jul 31, 2022
Collection of unusual generics usecases in Go

Unusual Generics Type parameters or Generics in Go designed to reduce boilerplate for container data types like lists, graphs, etc. and functions like

Vladimir Stolyarov 48 May 14, 2022
Package truthy provides truthy condition testing with Go generics

Truthy Truthy is a package which uses generics (Go 1.18+) to create useful boolean tests and helper functions. Examples // truthy.Value returns the tr

Carl Johnson 30 Jul 24, 2022
Go Library for Competitive Programming with Generics

Go Library for Competitive Programming with Generics Go used to be a difficult language to use for competitive programming. However, with the introduc

Katsuyuki Tateishi 2 Apr 19, 2022
A library that provides Go Generics friendly "optional" features.

go-optional A library that provides Go Generics friendly "optional" features. Synopsis some := optional.Some[int](123) fmt.Printf("%v\n", some.IsSome(

moznion 14 Jul 22, 2022
experimental promises in go1.18 with generics

async go a prototype of "promises" in go1.18. note: this is just an experiment used to test alternate patterns for dealing with asynchronous code in g

nick comer 58 Jul 31, 2022