Documentation ¶
Overview ¶
Package funcutil is an internal package providing utility functions to enable functional programming idioms in Go.
Index ¶
- func Compose[T any, S any, R any](f func(T) S, g func(S) R) func(T) R
- func Contains[T comparable](a []T, x T) bool
- func Curry2[T any, S any, R any](f func(T, S) R, x T) func(S) R
- func Curry3[T any, S any, R any, Q any](f func(T, S, R) Q, x T) func(S, R) Q
- func Exists[T any](a []T, f func(T) bool) bool
- func ExistsInMap[K comparable, T any](a map[K]T, f func(K, T) bool) bool
- func First[T any](x T, _ T) T
- func Iter[T any](a []T, f func(T))
- func Map[T any, S any](a []T, f func(T) S) []S
- func MapInPlace[T any](a []T, f func(T) T)
- func MapParallel[T any, S any](a []T, f func(T) S, numRoutines int) []S
- func MapValues[T comparable, S any](a map[T]S, f func(S) S) map[T]S
- func Merge[T comparable, S any](a map[T]S, b map[T]S, both func(x S, y S) S)
- func Retain[T comparable, R any](m map[T]R, keys []T)
- func Reverse[T any](a []T)
- func Second[T any](_ T, y T) T
- func Set[T comparable](a []T) map[T]struct{}
- func SetToOrderedSlice[T constraints.Ordered](set map[T]bool) []T
- func Union[T comparable](a map[T]bool, b map[T]bool) map[T]bool
- type Optional
- func BindOption[T any, S any](x Optional[T], f func(T) Optional[S]) Optional[S]
- func FindMap[T any, R any](a []T, f func(T) R, p func(R) bool) Optional[R]
- func MapOption[T any, S any](x Optional[T], f func(T) S) Optional[S]
- func MaybeOr[T any](x Optional[T], s Optional[T]) Optional[T]
- func None[T any]() Optional[T]
- func Some[T any](x T) Optional[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](a []T, x T) bool
Contains returns true when there is some y in slice a such that x == y
func Exists ¶
Exists returns true when there exists some x in slice a such that f(x), otherwise false.
func ExistsInMap ¶
func ExistsInMap[K comparable, T any](a map[K]T, f func(K, T) bool) bool
ExistsInMap returns true when there exists some k,x in map a such that f(k,x), otherwise false.
func Iter ¶
func Iter[T any](a []T, f func(T))
Iter iterates over all elements in the slice and call the function on that element.
func MapInPlace ¶
func MapInPlace[T any](a []T, f func(T) T)
MapInPlace iterates over all elements in the slice and call the function on that element to update it.
func MapParallel ¶
MapParallel is a parallel version of Map using numRoutines goroutines.
func MapValues ¶
func MapValues[T comparable, S any](a map[T]S, f func(S) S) map[T]S
MapValues returns a new slice b such for any i <= len(a), b[i] = f(a[i])
func Merge ¶
func Merge[T comparable, S any](a map[T]S, b map[T]S, both func(x S, y S) S)
Merge merges the two maps into the first map. if x is in b but not in a, then a[x] := b[x] if x in both in a and b, then a[x] := both(a[x], b[x]) @mutates a
func Retain ¶
func Retain[T comparable, R any](m map[T]R, keys []T)
Retain removes all the key-value pairs in m where the key is not in keys.
func Set ¶
func Set[T comparable](a []T) map[T]struct{}
Set takes an array of elements and returns a set of those elements, represented as a map from elements to empty struct
func SetToOrderedSlice ¶
func SetToOrderedSlice[T constraints.Ordered](set map[T]bool) []T
SetToOrderedSlice converts a set represented as a map from elements to booleans into a slice. Sorts the result in increasing order
Types ¶
type Optional ¶
type Optional[T any] interface { // ValueOr returns the value of the optional if it is some value, otherwise the default value if it is none ValueOr(defaultVal T) T // Value returns the value or panics if it is none Value() T // IsSome returns true if the optional represents some value IsSome() bool // IsNone returns true is the optional is none IsNone() bool }
An Optional holds a value or none. If it has a value, the IsSome returns true and IsNone returns false.
func BindOption ¶
BindOption is the monadic bind operation on optional values
func FindMap ¶
FindMap returns Some(f(x)) when there exists some x in slice a such that p(f(x)), otherwise None.