Root package github.com/rprtr258/fun provides common slice and functional utilities:
slice
Name
In
Out
Description
Map
func(T) R
func(T, int) R
...T
Apply function to all elements and get slice with results
Filter
func(T) bool
func(T, int) bool
...T
Filter slice elements using given predicate
FilterMap
func(T) (R, bool)
func(T, int) (R, bool)
...T
Transform each element, leaving only those for which true is returned
funcMapDict[T comparable, R any](collection []T, dict map[T]R) []RfuncMapErr[R, T any, E interface {errorcomparable}, FE interface {func(T) (R, E) |func(T, int) (R, E)}](f FE, slice ...T) ([]R, E)funcDeref[T any](ptr *T) TfuncMapToSlice[K comparable, V, R any](dict map[K]V, f func(K, V) R) []RfuncMapFilterToSlice[K comparable, V, R any](dict map[K]V, f func(K, V) (R, bool)) []RfuncKeys[K comparable, V any](dict map[K]V) []KfuncValues[K comparable, V any](dict map[K]V) []V// FindKeyBy returns the key of the first element predicate returns truthy for.funcFindKeyBy[K comparable, V any](dict map[K]V, predicate func(K, V) bool) (K, bool)// Uniq returns unique values of slice.funcUniq[T comparable](collection ...T) []T// Index returns first found element by predicate along with it's indexfuncIndex[T comparable](find func(T) bool, slice ...T) (T, int, bool)// Contains returns true if an element is present in a collection.funcContains[T comparable](needle T, slice ...T) bool// SliceToMap returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
// If any of two pairs would have the same key the last one gets added to the map.// The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.funcSliceToMap[K comparable, V, T any, F interface {func(T) (K, V) |func(T, int) (K, V)}](f F, slice ...T) map[K]V// FromMap makes slice of key/value pairs from map.funcFromMap[A comparable, B any](kv map[A]B) []Pair[A, B]// Copy slicefuncCopy[T any](slice []T) []T// ReverseInplace reverses slice in place.funcReverseInplace[A any](xs []A)// Subslice returns slice from start to end without panicking on out of boundsfuncSubslice[T any](start, end int, slice ...T) []T// Chunk divides slice into chunks of size chunkSizefuncChunk[T any](chunkSize int, slice ...T) [][]T// ConcatMap is like Map but concatenates resultsfuncConcatMap[T, R any](f func(T) []R, slice ...T) []R// All returns true if all elements satisfy the conditionfuncAll[T any](condition func(T) bool, slice ...T) bool// Any returns true if any element satisfies the conditionfuncAny[T any](condition func(T) bool, slice ...T) bool// SortBy sorts slice in place by given functionfuncSortBy[T any, R cmp.Ordered](by func(T) R, slice ...T)// GroupBy groups elements by keyfuncGroupBy[T any, K comparable](by func(T) K, slice ...T) map[K][]T
cmp
// Min returns the minimum of the given valuesfuncMin[T cmp.Ordered](xs ...T) T// Max returns the maximum of the given valuesfuncMax[T cmp.Ordered](xs ...T) T// Clamp returns x clamped between low and highfuncClamp[T cmp.Ordered](x, low, high T) T// MinBy returns the minimum of the given values using the given order functionfuncMinBy[T any, R cmp.Ordered](order func(T) R, xs ...T) T// MaxBy returns the maximum of the given values using the given order functionfuncMaxBy[T any, R cmp.Ordered](order func(T) R, xs ...T) T
optional values
// Option is either value or nothing.typeOption[T any] struct { Value T Valid bool}funcInvalid[T any]() Option[T] {returnOption[T]{}}funcValid[T any](t T) Option[T] {returnOption[T]{ Value: t, Valid: true, }}funcOptional[T any](value T, valid bool) Option[T]func (o Option[T]) Unpack() (T, bool)func (o Option[T]) Or(other Option[T]) Option[T]func (o Option[T]) OrDefault(value T) TfuncFromPtr[T any](ptr *T) Option[T]func (opt Option[T]) Ptr() *TfuncOptMap[I, O any](o Option[I], f func(I) O) Option[O]funcOptFlatMap[I, O any](o Option[I], f func(I) Option[O]) Option[O]
fp
// Pair is a data structure that has two values.typePair[K, V any] struct {K K; V V}funcZero[T any]() T// ToString converts the value to string.funcToString[A any](a A) string {// DebugP returns function that prints prefix with element and returns it.// Useful for debug printing.funcDebugP[V any](prefix string) func(V) V// Debug returns function that prints element and returns it.// Useful for debug printing.funcDebug[V any](v V) VfuncHas[K comparable, V any](dict map[K]V, key K) boolfuncCond[R any](defaultValue R, cases ...func() (R, bool)) RfuncPtr[T any](t T) *TfuncPipe[T any](t T, endos ...func(T) T) T
functional if
funcIF[T any](predicate bool, ifTrue, ifFalse T) TfuncIf[T any](predicate bool, value T) ifElse[T]funcIfF[T any](predicate bool, value func() T) ifElse[T]func (i ifElse[T]) ElseIf(predicate bool, value T) ifElse[T]func (i ifElse[T]) ElseIfF(predicate bool, value func() T) ifElse[T]func (i ifElse[T]) Else(value T) Tfunc (i ifElse[T]) ElseF(value func() T) Tfunc (i ifElse[T]) ElseDeref(value *T) T
functional switch
// Switch is a pure functional switch/case/default statement.funcSwitch[R any, T comparable](predicate T, defVal R) *switchCase[T, R]// SwitchZero is a pure functional switch/case/default statement with default// zero value.funcSwitchZero[R any, T comparable](predicate T) *switchCase[T, R]func (s *switchCase[T, R]) Case(val T, result R) *switchCase[T, R]func (s *switchCase[T, R]) End() R
Iterators
github.com/rprtr258/fun/iter introduces iterator primitives from which Seq[T] is basic:
typeSeq[V any] func(yield func(V) bool) bool
Which is a function which accepts function to yield values from iteration. yield must return false when iteration must stop (analogous to break). Iterator function returns true if it yielded all values and no break happened, or false otherwise.
Example iterator yielding numbers from 1 to n, including n:
funcRange(n int) iter.Seq[int] {returnfunc(yield func(int) bool) bool {for i :=1; i <= n; i++ {if!yield(i) {returnfalse } }returntrue }}
Set
github.com/rprtr258/fun/set introduces Set[T] primitive for collections of unique comparable values.