go-fun

module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2022 License: Apache-2.0

README

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)
}

Directories

Path Synopsis
Package dict provides implementations of immutable dictionaries.
Package dict provides implementations of immutable dictionaries.
arraydict
Package arraydict implements an immutable dictionary based on an array of dict.Entry.
Package arraydict implements an immutable dictionary based on an array of dict.Entry.
hashdict
Package hashdict implements a dictionary based on a hash-trie data structure.
Package hashdict implements a dictionary based on a hash-trie data structure.
Package equality provides a type class for equality with some commonly used instances:
Package equality provides a type class for equality with some commonly used instances:
Package hash provides a type class for hashable types, and defines some commonly used instances.
Package hash provides a type class for hashable types, and defines some commonly used instances.
internal
mutable
Package mutable provides mutable data types, which are used internally to implement the functional algorithms and data types.
Package mutable provides mutable data types, which are used internally to implement the functional algorithms and data types.
Package iterable provides an interface Iterable for types that can be iterated over.
Package iterable provides an interface Iterable for types that can be iterated over.
Package list implements an immutable list that is backed by a slice.
Package list implements an immutable list that is backed by a slice.
linked
Package linked implements an immutable singly-linked list.
Package linked implements an immutable singly-linked list.
Package opt provides an implementation of the Optional data type.
Package opt provides an implementation of the Optional data type.
Package reducer implements reducers, similar to the Reducer concept in Clojure (https://clojure.org/reference/reducers).
Package reducer implements reducers, similar to the Reducer concept in Clojure (https://clojure.org/reference/reducers).
set
Package set contains implementations of immutable set data structures.
Package set contains implementations of immutable set data structures.
hashset
Package hashset implements an immutable set by using a hashdict in the background.
Package hashset implements an immutable set by using a hashdict in the background.
Package slice returns generic functions to work with slices.
Package slice returns generic functions to work with slices.
Package zero returns an utility function to return the zero value for a type.
Package zero returns an utility function to return the zero value for a type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL