Skip to content

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.

License

Notifications You must be signed in to change notification settings

peterzeller/go-fun

Repository files navigation

Go-fun

pipeline status Coverage Status Go Reference

Utilities and immutable collections for functional programming in Golang.

For documentation, check the Go Package Documentation.

Features

  • Immutable data structures
  • Iterable abstraction (package iterable)
  • Reducers for transforming data (map, filter, group by, etc) (package reducer)
  • Equality type class (package equality)
  • Hash type class (package hash)
  • Generic Zero Value (package zero)
  • Generic Slice functions (package slice)
  • Mutable data structures (package mutable)
    • Stack

Why immutable collections?

  • Immutable data is thread-safe by default
  • Immutable data structures are more efficient than copying data to prevent unwanted modification
  • Use immutable data structures at API boundaries to make it clear that data cannot be modified.
  • Updated versions of a data structure share underlying state, which makes them more memory efficient when keeping multiple versions (in recursive algorithms, in search algorithms, for undo/history functionality)

Examples

Immutable Dict

d0 := hashdict.New[string, int](hash.String())

d1 := d0.Set("a", 1)
d2 := d1.Set("b", 42)
d3 := d2.Set("a", 7)

require.Equal(t, 1, d1.GetOrZero("a"))
require.Equal(t, 1, d2.GetOrZero("a"))
require.Equal(t, 7, d3.GetOrZero("a"))

require.Equal(t, 0, d1.GetOrZero("b"))
require.Equal(t, 42, d2.GetOrZero("b"))
require.Equal(t, 42, d3.GetOrZero("b"))

Immutable Set

Reducers

Take a slice of books and return a map where the keys are authors and the values contain all the titles of the books the author has written in or after the year 2000.

type Book struct {
	Author string
	Title  string
	Year   int
}

func TestBook(t *testing.T) {
	books := []Book{
		{"A", "Q", 1990},
		{"B", "R", 2005},
		{"A", "S", 2001},
		{"B", "T", 1999},
		{"B", "U", 2021},
	}

	m := reducer.ApplySlice(books,
		reducer.Filter(func(b Book) bool { return b.Year >= 2000 },
			reducer.GroupBy(func(b Book) string { return b.Author },
				reducer.Map(func(b Book) string { return b.Title }, reducer.ToSlice[string]()))))

	require.Equal(t, map[string][]string{
		"A": {"S"},
		"B": {"R", "U"},
	}, m)
}

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published