Documentation
¶
Overview ¶
Package record contains monadic operations for maps as well as a rich set of utility functions
Index ¶
- func Ap[A any, K comparable, B any](m Monoid[Record[K, B]]) func(fa Record[K, A]) Operator[K, func(A) B, B]
- func ApS[S1, T any, K comparable, S2 any](m Monoid[Record[K, S2]]) func(setter func(T) func(S1) S2, fa Record[K, T]) Operator[K, S1, S2]
- func Bind[S1, T any, K comparable, S2 any](m Monoid[Record[K, S2]]) func(setter func(T) func(S1) S2, f Kleisli[K, S1, T]) Operator[K, S1, S2]
- func Chain[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]]) func(Kleisli[K, V1, V2]) Operator[K, V1, V2]
- func ChainWithIndex[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]]) func(KleisliWithIndex[K, V1, V2]) Operator[K, V1, V2]
- func Collect[K comparable, V, R any](f func(K, V) R) func(Record[K, V]) []R
- func CollectOrd[V, R any, K comparable](o ord.Ord[K]) func(func(K, V) R) func(Record[K, V]) []R
- func Eq[K comparable, V any](e E.Eq[V]) E.Eq[Record[K, V]]
- func FilterChain[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]]) func(option.Kleisli[V1, Record[K, V2]]) Operator[K, V1, V2]
- func FilterChainWithIndex[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]]) func(func(K, V1) Option[Record[K, V2]]) Operator[K, V1, V2]
- func Flatten[K comparable, V any](m Monoid[Record[K, V]]) func(Record[K, Record[K, V]]) Record[K, V]
- func Fold[K comparable, A any](m Monoid[A]) func(Record[K, A]) A
- func FoldMap[K comparable, A, B any](m Monoid[B]) func(func(A) B) func(Record[K, A]) B
- func FoldMapOrd[A, B any, K comparable](o ord.Ord[K]) func(m Monoid[B]) func(func(A) B) func(Record[K, A]) B
- func FoldMapOrdWithIndex[K comparable, A, B any](o ord.Ord[K]) func(m Monoid[B]) func(func(K, A) B) func(Record[K, A]) B
- func FoldMapWithIndex[K comparable, A, B any](m Monoid[B]) func(func(K, A) B) func(Record[K, A]) B
- func FoldOrd[A any, K comparable](o ord.Ord[K]) func(m Monoid[A]) func(Record[K, A]) A
- func FromArrayMap[A any, K comparable, V any](m Mg.Magma[V]) func(f func(A) Entry[K, V]) Kleisli[K, []A, V]
- func FromFoldableMap[...](m Mg.Magma[V], red FOLDABLE) func(f func(A) Entry[K, V]) Kleisli[K, HKTA, V]
- func FromStrictEquals[K, V comparable]() E.Eq[Record[K, V]]
- func Has[K comparable, V any](k K, r Record[K, V]) bool
- func IsEmpty[K comparable, V any](r Record[K, V]) bool
- func IsNil[K comparable, V any](m Record[K, V]) bool
- func IsNonEmpty[K comparable, V any](r Record[K, V]) bool
- func IsNonNil[K comparable, V any](m Record[K, V]) bool
- func Keys[K comparable, V any](r Record[K, V]) []K
- func KeysOrd[V any, K comparable](o ord.Ord[K]) func(r Record[K, V]) []K
- func Lookup[V any, K comparable](k K) option.Kleisli[Record[K, V], V]
- func Reduce[K comparable, V, R any](f func(R, V) R, initial R) func(Record[K, V]) R
- func ReduceOrd[V, R any, K comparable](o ord.Ord[K]) func(func(R, V) R, R) func(Record[K, V]) R
- func ReduceOrdWithIndex[V, R any, K comparable](o ord.Ord[K]) func(func(K, R, V) R, R) func(Record[K, V]) R
- func ReduceRef[K comparable, V, R any](f func(R, *V) R, initial R) func(Record[K, V]) R
- func ReduceRefWithIndex[K comparable, V, R any](f func(K, R, *V) R, initial R) func(Record[K, V]) R
- func ReduceWithIndex[K comparable, V, R any](f func(K, R, V) R, initial R) func(Record[K, V]) R
- func Sequence[K comparable, A, HKTA, HKTAA, HKTRA any](fof func(map[K]A) HKTRA, ...) HKTRA
- func Size[K comparable, V any](r Record[K, V]) int
- func Traverse[K comparable, A, B, HKTB, HKTAB, HKTRB any](fof func(map[K]B) HKTRB, ...) func(map[K]A) HKTRB
- func TraverseWithIndex[K comparable, A, B, HKTB, HKTAB, HKTRB any](fof func(map[K]B) HKTRB, ...) func(map[K]A) HKTRB
- func Union[K comparable, V any](m Mg.Magma[V]) func(Record[K, V]) Operator[K, V, V]
- func Values[K comparable, V any](r Record[K, V]) []V
- func ValuesOrd[V any, K comparable](o ord.Ord[K]) func(r Record[K, V]) []V
- type Collector
- type Endomorphism
- type Entries
- type Entry
- type Kleisli
- type KleisliWithIndex
- type Monoid
- type Operator
- func BindTo[S1, T any, K comparable](setter func(T) S1) Operator[K, T, S1]
- func DeleteAt[K comparable, V any](k K) Operator[K, V, V]
- func Filter[K comparable, V any](f Predicate[K]) Operator[K, V, V]
- func FilterMap[K comparable, V1, V2 any](f option.Kleisli[V1, V2]) Operator[K, V1, V2]
- func FilterMapWithIndex[K comparable, V1, V2 any](f func(K, V1) Option[V2]) Operator[K, V1, V2]
- func FilterWithIndex[K comparable, V any](f PredicateWithIndex[K, V]) Operator[K, V, V]
- func Flap[B any, K comparable, A any](a A) Operator[K, func(A) B, B]
- func Let[S1, T any, K comparable, S2 any](setter func(T) func(S1) S2, f func(S1) T) Operator[K, S1, S2]
- func LetTo[S1, T any, K comparable, S2 any](setter func(T) func(S1) S2, b T) Operator[K, S1, S2]
- func Map[K comparable, V, R any](f func(V) R) Operator[K, V, R]
- func MapRef[K comparable, V, R any](f func(*V) R) Operator[K, V, R]
- func MapRefWithIndex[K comparable, V, R any](f func(K, *V) R) Operator[K, V, R]
- func MapWithIndex[K comparable, V, R any](f func(K, V) R) Operator[K, V, R]
- func Merge[K comparable, V any](right Record[K, V]) Operator[K, V, V]
- func UpsertAt[K comparable, V any](k K, v V) Operator[K, V, V]
- type OperatorWithIndex
- type Option
- type Predicate
- type PredicateWithIndex
- type Record
- func ConstNil[K comparable, V any]() Record[K, V]
- func Copy[K comparable, V any](m Record[K, V]) Record[K, V]
- func Do[K comparable, S any]() Record[K, S]
- func Empty[K comparable, V any]() Record[K, V]
- func FromEntries[K comparable, V any](fa Entries[K, V]) Record[K, V]
- func MonadAp[A any, K comparable, B any](m Monoid[Record[K, B]], fab Record[K, func(A) B], fa Record[K, A]) Record[K, B]
- func MonadChain[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]], r Record[K, V1], f Kleisli[K, V1, V2]) Record[K, V2]
- func MonadChainWithIndex[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]], r Record[K, V1], f KleisliWithIndex[K, V1, V2]) Record[K, V2]
- func MonadFlap[B any, K comparable, A any](fab Record[K, func(A) B], a A) Record[K, B]
- func MonadMap[K comparable, V, R any](r Record[K, V], f func(V) R) Record[K, R]
- func MonadMapRef[K comparable, V, R any](r Record[K, V], f func(*V) R) Record[K, R]
- func MonadMapRefWithIndex[K comparable, V, R any](r Record[K, V], f func(K, *V) R) Record[K, R]
- func MonadMapWithIndex[K comparable, V, R any](r Record[K, V], f func(K, V) R) Record[K, R]
- func Of[K comparable, A any](k K, a A) Record[K, A]
- func Singleton[K comparable, V any](k K, v V) Record[K, V]
- type Reducer
- type ReducerWithIndex
- type Semigroup
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Ap ¶
func Ap[A any, K comparable, B any](m Monoid[Record[K, B]]) func(fa Record[K, A]) Operator[K, func(A) B, B]
Ap returns a function that applies a map of functions to a map of values, combining results using the provided Monoid
func ApS ¶
func ApS[S1, T any, K comparable, S2 any](m Monoid[Record[K, S2]]) func( setter func(T) func(S1) S2, fa Record[K, T], ) Operator[K, S1, S2]
ApS attaches a value to a context [S1] to produce a context [S2] by considering the context and the value concurrently (using Applicative rather than Monad). This allows independent computations to be combined without one depending on the result of the other.
Unlike Bind, which sequences operations, ApS can be used when operations are independent and can conceptually run in parallel.
Example:
type State struct {
Name string
Count int
}
// These operations are independent and can be combined with ApS
names := map[string]string{"a": "Alice", "b": "Bob"}
counts := map[string]int{"a": 10, "b": 20}
result := F.Pipe2(
record.Do[string, State](),
record.ApS(monoid.Record[string, State]())(
func(name string) func(State) State {
return func(s State) State { s.Name = name; return s }
},
names,
),
record.ApS(monoid.Record[string, State]())(
func(count int) func(State) State {
return func(s State) State { s.Count = count; return s }
},
counts,
),
) // map[string]State{"a": {Name: "Alice", Count: 10}, "b": {Name: "Bob", Count: 20}}
func Bind ¶
func Bind[S1, T any, K comparable, S2 any](m Monoid[Record[K, S2]]) func( setter func(T) func(S1) S2, f Kleisli[K, S1, T], ) Operator[K, S1, S2]
Bind attaches the result of a computation to a context [S1] to produce a context [S2]. This enables sequential composition where each step can depend on the results of previous steps. For records, this merges values by key.
The setter function takes the result of the computation and returns a function that updates the context from S1 to S2.
Example:
type State struct {
Name string
Count int
}
result := F.Pipe2(
record.Do[string, State](),
record.Bind(monoid.Record[string, State]())(
func(name string) func(State) State {
return func(s State) State { s.Name = name; return s }
},
func(s State) map[string]string {
return map[string]string{"a": "Alice", "b": "Bob"}
},
),
record.Bind(monoid.Record[string, State]())(
func(count int) func(State) State {
return func(s State) State { s.Count = count; return s }
},
func(s State) map[string]int {
// This can access s.Name from the previous step
return map[string]int{"a": len(s.Name), "b": len(s.Name) * 2}
},
),
)
func Chain ¶
func Chain[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]]) func(Kleisli[K, V1, V2]) Operator[K, V1, V2]
Chain returns a function that chains a map transformation function that produces maps, combining results using the provided Monoid
func ChainWithIndex ¶
func ChainWithIndex[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]]) func(KleisliWithIndex[K, V1, V2]) Operator[K, V1, V2]
ChainWithIndex returns a function that chains a map transformation function that produces maps, combining results using the provided Monoid
func Collect ¶
func Collect[K comparable, V, R any](f func(K, V) R) func(Record[K, V]) []R
Collect applies a collector function to the key value pairs in a map and returns the result as an array
func CollectOrd ¶
func CollectOrd[V, R any, K comparable](o ord.Ord[K]) func(func(K, V) R) func(Record[K, V]) []R
CollectOrd applies a collector function to the key value pairs in a map and returns the result as an array
func FilterChain ¶
func FilterChain[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]]) func(option.Kleisli[V1, Record[K, V2]]) Operator[K, V1, V2]
FilterChain creates a new map with only the elements for which the transformation function creates a Some
func FilterChainWithIndex ¶
func FilterChainWithIndex[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]]) func(func(K, V1) Option[Record[K, V2]]) Operator[K, V1, V2]
FilterChainWithIndex creates a new map with only the elements for which the transformation function creates a Some
func Flatten ¶
func Flatten[K comparable, V any](m Monoid[Record[K, V]]) func(Record[K, Record[K, V]]) Record[K, V]
Flatten converts a nested map into a regular map
func Fold ¶
func Fold[K comparable, A any](m Monoid[A]) func(Record[K, A]) A
Fold folds the record using the provided Monoid.
func FoldMap ¶
func FoldMap[K comparable, A, B any](m Monoid[B]) func(func(A) B) func(Record[K, A]) B
FoldMap maps and folds a record. Map the record passing each value to the iterating function. Then fold the results using the provided Monoid.
Example ¶
src := map[string]string{
"a": "a",
"b": "b",
"c": "c",
}
fold := FoldMapOrd[string, string](S.Ord)(S.Monoid)(strings.ToUpper)
fmt.Println(fold(src))
Output: ABC
func FoldMapOrd ¶
func FoldMapOrd[A, B any, K comparable](o ord.Ord[K]) func(m Monoid[B]) func(func(A) B) func(Record[K, A]) B
FoldMap maps and folds a record. Map the record passing each value to the iterating function. Then fold the results using the provided Monoid and the items in the provided order
func FoldMapOrdWithIndex ¶
func FoldMapOrdWithIndex[K comparable, A, B any](o ord.Ord[K]) func(m Monoid[B]) func(func(K, A) B) func(Record[K, A]) B
FoldMapWithIndex maps and folds a record. Map the record passing each value to the iterating function. Then fold the results using the provided Monoid and the items in the provided order
func FoldMapWithIndex ¶
func FoldMapWithIndex[K comparable, A, B any](m Monoid[B]) func(func(K, A) B) func(Record[K, A]) B
FoldMapWithIndex maps and folds a record. Map the record passing each value to the iterating function. Then fold the results using the provided Monoid.
func FoldOrd ¶
func FoldOrd[A any, K comparable](o ord.Ord[K]) func(m Monoid[A]) func(Record[K, A]) A
Fold folds the record using the provided Monoid with the items passed in the given order
func FromArrayMap ¶
func FromArrayMap[ A any, K comparable, V any](m Mg.Magma[V]) func(f func(A) Entry[K, V]) Kleisli[K, []A, V]
FromArrayMap converts from an array to a map Duplicate keys are resolved by the provided [Mg.Magma]
func FromFoldableMap ¶
func FromFoldableMap[ FOLDABLE ~func(func(Record[K, V], A) Record[K, V], Record[K, V]) func(HKTA) Record[K, V], A any, HKTA any, K comparable, V any](m Mg.Magma[V], red FOLDABLE) func(f func(A) Entry[K, V]) Kleisli[K, HKTA, V]
FromFoldableMap converts from a reducer to a map Duplicate keys are resolved by the provided [Mg.Magma]
func FromStrictEquals ¶
func FromStrictEquals[K, V comparable]() E.Eq[Record[K, V]]
FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
func Has ¶
func Has[K comparable, V any](k K, r Record[K, V]) bool
Has tests if a key is contained in a map
func IsEmpty ¶
func IsEmpty[K comparable, V any](r Record[K, V]) bool
IsEmpty tests if a map is empty
func IsNil ¶
func IsNil[K comparable, V any](m Record[K, V]) bool
IsNil checks if the map is set to nil
func IsNonEmpty ¶
func IsNonEmpty[K comparable, V any](r Record[K, V]) bool
IsNonEmpty tests if a map is not empty
func IsNonNil ¶
func IsNonNil[K comparable, V any](m Record[K, V]) bool
IsNonNil checks if the map is set to nil
func KeysOrd ¶
func KeysOrd[V any, K comparable](o ord.Ord[K]) func(r Record[K, V]) []K
KeysOrd returns the keys in the map in their given order
func Lookup ¶
func Lookup[V any, K comparable](k K) option.Kleisli[Record[K, V], V]
Lookup returns the entry for a key in a map if it exists
func Reduce ¶
func Reduce[K comparable, V, R any](f func(R, V) R, initial R) func(Record[K, V]) R
Reduce reduces a map to a single value by applying a reducer function to each value
func ReduceOrd ¶
func ReduceOrd[V, R any, K comparable](o ord.Ord[K]) func(func(R, V) R, R) func(Record[K, V]) R
ReduceOrd reduces a map into a single value via a reducer function making sure that the keys are passed to the reducer in the specified order
func ReduceOrdWithIndex ¶
func ReduceOrdWithIndex[V, R any, K comparable](o ord.Ord[K]) func(func(K, R, V) R, R) func(Record[K, V]) R
ReduceOrdWithIndex reduces a map into a single value via a reducer function making sure that the keys are passed to the reducer in the specified order
func ReduceRef ¶
func ReduceRef[K comparable, V, R any](f func(R, *V) R, initial R) func(Record[K, V]) R
ReduceRef reduces a map to a single value by applying a reducer function to each value reference
func ReduceRefWithIndex ¶
func ReduceRefWithIndex[K comparable, V, R any](f func(K, R, *V) R, initial R) func(Record[K, V]) R
ReduceRefWithIndex reduces a map to a single value by applying a reducer function to each key-value pair with value references
func ReduceWithIndex ¶
func ReduceWithIndex[K comparable, V, R any](f func(K, R, V) R, initial R) func(Record[K, V]) R
ReduceWithIndex reduces a map to a single value by applying a reducer function to each key-value pair
func Sequence ¶
func Sequence[K comparable, A, HKTA, HKTAA, HKTRA any]( fof func(map[K]A) HKTRA, fmap func(func(map[K]A) func(A) map[K]A) func(HKTRA) HKTAA, fap func(HKTA) func(HKTAA) HKTRA, ma map[K]HKTA) HKTRA
Sequence transforms a map of effects into an effect of a map. This is the dual of Traverse where the transformation function is the identity.
This is useful when you have a map where each value is already in an effect context (like Option, Either, etc.) and you want to "flip" the nesting to get a single effect containing a map of plain values.
If any value in the map is a "failure" (e.g., None, Left), the entire result will be a failure. If all values are "successes", the result will be a success containing a map of all the unwrapped values.
Type parameters:
- K: The key type (must be comparable)
- A: The value type inside the effect
- HKTA: Higher-kinded type representing the effect containing A (e.g., Option[A])
- HKTAA: Higher-kinded type representing a function from A to map[K]A in the effect
- HKTRA: Higher-kinded type representing the effect containing map[K]A
Parameters:
- fof: Lifts a pure map[K]A into the effect (the "of" or "pure" function)
- fmap: Maps a function over the effect (the "map" or "fmap" function)
- fap: Applies an effectful function to an effectful value (the "ap" function)
- ma: The input map where each value is in an effect context
Example with Option:
input := map[string]O.Option[int]{"a": O.Some(1), "b": O.Some(2)}
result := Sequence(O.Of[map[string]int], O.Map[...], O.Ap[...], input)
// result: O.Some(map[string]int{"a": 1, "b": 2})
input2 := map[string]O.Option[int]{"a": O.Some(1), "b": O.None[int]()}
result2 := Sequence(O.Of[map[string]int], O.Map[...], O.Ap[...], input2)
// result2: O.None[map[string]int]()
func Size ¶
func Size[K comparable, V any](r Record[K, V]) int
Size returns the number of elements in a map
func Traverse ¶
func Traverse[K comparable, A, B, HKTB, HKTAB, HKTRB any]( fof func(map[K]B) HKTRB, fmap func(func(map[K]B) func(B) map[K]B) func(HKTRB) HKTAB, fap func(HKTB) func(HKTAB) HKTRB, f func(A) HKTB) func(map[K]A) HKTRB
Traverse transforms a map of values into a value of a map by applying an effectful function to each value. Unlike TraverseWithIndex, this function does not provide access to the keys.
This is useful when you need to perform an operation that may fail or have side effects on each element of a map, and you want to collect the results in the same applicative context.
Type parameters:
- K: The key type (must be comparable)
- A: The input value type
- B: The output value type
- HKTB: Higher-kinded type representing the effect containing B (e.g., Option[B], Either[E, B])
- HKTAB: Higher-kinded type representing a function from B to map[K]B in the effect
- HKTRB: Higher-kinded type representing the effect containing map[K]B
Parameters:
- fof: Lifts a pure map[K]B into the effect (the "of" or "pure" function)
- fmap: Maps a function over the effect (the "map" or "fmap" function)
- fap: Applies an effectful function to an effectful value (the "ap" function)
- f: The transformation function that takes a value and returns an effect
Example with Option:
f := func(s string) O.Option[string] {
if s != "" {
return O.Some(strings.ToUpper(s))
}
return O.None[string]()
}
traverse := Traverse(O.Of[map[string]string], O.Map[...], O.Ap[...], f)
result := traverse(map[string]string{"a": "hello"}) // O.Some(map[string]string{"a": "HELLO"})
func TraverseWithIndex ¶
func TraverseWithIndex[K comparable, A, B, HKTB, HKTAB, HKTRB any]( fof func(map[K]B) HKTRB, fmap func(func(map[K]B) func(B) map[K]B) func(HKTRB) HKTAB, fap func(HKTB) func(HKTAB) HKTRB, f func(K, A) HKTB) func(map[K]A) HKTRB
TraverseWithIndex transforms a map of values into a value of a map by applying an effectful function to each key-value pair. The function has access to both the key and value.
This is useful when you need to perform an operation that may fail or have side effects on each element of a map, and you want to collect the results in the same applicative context.
Type parameters:
- K: The key type (must be comparable)
- A: The input value type
- B: The output value type
- HKTB: Higher-kinded type representing the effect containing B (e.g., Option[B], Either[E, B])
- HKTAB: Higher-kinded type representing a function from B to map[K]B in the effect
- HKTRB: Higher-kinded type representing the effect containing map[K]B
Parameters:
- fof: Lifts a pure map[K]B into the effect (the "of" or "pure" function)
- fmap: Maps a function over the effect (the "map" or "fmap" function)
- fap: Applies an effectful function to an effectful value (the "ap" function)
- f: The transformation function that takes a key and value and returns an effect
Example with Option:
f := func(k string, n int) O.Option[int] {
if n > 0 {
return O.Some(n * 2)
}
return O.None[int]()
}
traverse := TraverseWithIndex(O.Of[map[string]int], O.Map[...], O.Ap[...], f)
result := traverse(map[string]int{"a": 1, "b": 2}) // O.Some(map[string]int{"a": 2, "b": 4})
func Union ¶
func Union[K comparable, V any](m Mg.Magma[V]) func(Record[K, V]) Operator[K, V, V]
Union combines two maps using the provided Magma to resolve conflicts for duplicate keys
func Values ¶
func Values[K comparable, V any](r Record[K, V]) []V
Values returns the values in a map
func ValuesOrd ¶
func ValuesOrd[V any, K comparable](o ord.Ord[K]) func(r Record[K, V]) []V
ValuesOrd returns the values in the map ordered by their keys in the given order
Example ¶
src := map[string]string{
"c": "a",
"b": "b",
"a": "c",
}
getValues := ValuesOrd[string](S.Ord)
fmt.Println(getValues(src))
Output: [c b a]
Types ¶
type Collector ¶ added in v2.0.1
type Collector[K comparable, V, R any] = func(K, V) R
Collector transforms key-value pairs into a result type and collects them into an array.
Example:
toStrings := Collect(func(k string, v int) string {
return fmt.Sprintf("%s=%d", k, v)
})
type Endomorphism ¶ added in v2.0.2
type Endomorphism[A any] = endomorphism.Endomorphism[A]
Endomorphism represents a function from a type to itself (A -> A).
func Clone ¶
func Clone[K comparable, V any](f Endomorphism[V]) Endomorphism[Record[K, V]]
Clone creates a deep copy of the map using the provided endomorphism to clone the values
type Entries ¶ added in v2.0.1
type Entries[K comparable, V any] = []Entry[K, V]
Entries is a slice of key-value pairs.
Example:
entries := Entries[string, int]{
T.MakeTuple2("a", 1),
T.MakeTuple2("b", 2),
}
record := FromEntries(entries)
func ToArray ¶
func ToArray[K comparable, V any](r Record[K, V]) Entries[K, V]
ToArray converts a map to an array of key-value pairs
func ToEntries ¶
func ToEntries[K comparable, V any](r Record[K, V]) Entries[K, V]
ToEntries converts a map to an array of key-value pairs (alias for ToArray)
type Entry ¶ added in v2.0.1
type Entry[K comparable, V any] = pair.Pair[K, V]
Entry represents a single key-value pair from a record. This is an alias for Tuple2 to provide semantic clarity.
Example:
entries := ToEntries(record)
for _, entry := range entries {
key := entry.F1
value := entry.F2
}
type Kleisli ¶ added in v2.0.1
type Kleisli[K comparable, V1, V2 any] = func(V1) Record[K, V2]
Kleisli represents a monadic function that transforms a value into a record. Used in chain operations for composing record-producing functions.
Example:
expand := func(x int) Record[string, int] {
return Record[string, int]{
"double": x * 2,
"triple": x * 3,
}
}
func FromArray ¶
func FromArray[ K comparable, V any](m Mg.Magma[V]) Kleisli[K, Entries[K, V], V]
FromArray converts from an array to a map Duplicate keys are resolved by the provided [Mg.Magma]
func FromFoldable ¶
func FromFoldable[ HKTA any, FOLDABLE ~func(func(Record[K, V], Entry[K, V]) Record[K, V], Record[K, V]) func(HKTA) Record[K, V], K comparable, V any](m Mg.Magma[V], red FOLDABLE) Kleisli[K, HKTA, V]
FromFoldable converts from a reducer to a map Duplicate keys are resolved by the provided [Mg.Magma]
type KleisliWithIndex ¶ added in v2.0.1
type KleisliWithIndex[K comparable, V1, V2 any] = func(K, V1) Record[K, V2]
KleisliWithIndex is a monadic function that uses both key and value to produce a record.
Example:
expandWithKey := func(k string, v int) Record[string, int] {
return Record[string, int]{
k + "_double": v * 2,
k + "_triple": v * 3,
}
}
type Monoid ¶ added in v2.0.2
Monoid represents a monoid structure with an associative binary operation and identity element.
func MergeMonoid ¶
func MergeMonoid[K comparable, V any]() Monoid[Record[K, V]]
MergeMonoid computes the union of two maps of the same type giving the last map precedence
func UnionFirstMonoid ¶
func UnionFirstMonoid[K comparable, V any]() Monoid[Record[K, V]]
UnionFirstMonoid computes the union of two maps of the same type giving the first map precedence
func UnionLastMonoid ¶
func UnionLastMonoid[K comparable, V any]() Monoid[Record[K, V]]
UnionLastMonoid computes the union of two maps of the same type giving the last map precedence
func UnionMonoid ¶
func UnionMonoid[K comparable, V any](s S.Semigroup[V]) Monoid[Record[K, V]]
UnionMonoid computes the union of two maps of the same type
type Operator ¶ added in v2.0.1
type Operator[K comparable, V1, V2 any] = func(Record[K, V1]) Record[K, V2]
Operator transforms a record from one value type to another while preserving keys. This is the fundamental transformation type for record operations.
Example:
doubleValues := Map(func(x int) int { return x * 2 })
result := doubleValues(Record[string, int]{"a": 1, "b": 2})
// result: {"a": 2, "b": 4}
func BindTo ¶
func BindTo[S1, T any, K comparable](setter func(T) S1) Operator[K, T, S1]
BindTo initializes a new state [S1] from a value [T]. This is typically used as the first step in a do-notation chain to convert a simple map of values into a map of state objects.
Example:
type State struct {
Name string
}
result := F.Pipe1(
map[string]string{"a": "Alice", "b": "Bob"},
record.BindTo(func(name string) State { return State{Name: name} }),
) // map[string]State{"a": {Name: "Alice"}, "b": {Name: "Bob"}}
func DeleteAt ¶
func DeleteAt[K comparable, V any](k K) Operator[K, V, V]
DeleteAt returns a function that removes a key from a map
func Filter ¶
func Filter[K comparable, V any](f Predicate[K]) Operator[K, V, V]
Filter creates a new map with only the elements that match the predicate
func FilterMap ¶
func FilterMap[K comparable, V1, V2 any](f option.Kleisli[V1, V2]) Operator[K, V1, V2]
FilterMap creates a new map with only the elements for which the transformation function creates a Some
func FilterMapWithIndex ¶
func FilterMapWithIndex[K comparable, V1, V2 any](f func(K, V1) Option[V2]) Operator[K, V1, V2]
FilterMapWithIndex creates a new map with only the elements for which the transformation function creates a Some
func FilterWithIndex ¶
func FilterWithIndex[K comparable, V any](f PredicateWithIndex[K, V]) Operator[K, V, V]
FilterWithIndex creates a new map with only the elements that match the predicate
func Flap ¶
func Flap[B any, K comparable, A any](a A) Operator[K, func(A) B, B]
Flap returns a function that applies a value to a map of functions, producing a map of results
func Let ¶
func Let[S1, T any, K comparable, S2 any]( setter func(T) func(S1) S2, f func(S1) T, ) Operator[K, S1, S2]
Let attaches the result of a computation to a context [S1] to produce a context [S2]. Unlike Bind, Let does not require a Monoid because it transforms each value independently without merging multiple maps.
The setter function takes the computed value and returns a function that updates the context. The computation function f takes the current context and produces a value.
Example:
type State struct {
Name string
Length int
}
result := F.Pipe2(
map[string]State{"a": {Name: "Alice"}},
record.Let(
func(length int) func(State) State {
return func(s State) State { s.Length = length; return s }
},
func(s State) int { return len(s.Name) },
),
) // map[string]State{"a": {Name: "Alice", Length: 5}}
func LetTo ¶
func LetTo[S1, T any, K comparable, S2 any]( setter func(T) func(S1) S2, b T, ) Operator[K, S1, S2]
LetTo attaches a constant value to a context [S1] to produce a context [S2]. This is similar to Let but uses a fixed value instead of computing it from the context.
The setter function takes the value and returns a function that updates the context.
Example:
type State struct {
Name string
Version int
}
result := F.Pipe2(
map[string]State{"a": {Name: "Alice"}},
record.LetTo(
func(version int) func(State) State {
return func(s State) State { s.Version = version; return s }
},
2,
),
) // map[string]State{"a": {Name: "Alice", Version: 2}}
func Map ¶
func Map[K comparable, V, R any](f func(V) R) Operator[K, V, R]
Map returns a function that transforms each value in a map using the provided function
func MapRef ¶
func MapRef[K comparable, V, R any](f func(*V) R) Operator[K, V, R]
MapRef returns a function that transforms each value in a map using the provided function with value references
func MapRefWithIndex ¶
func MapRefWithIndex[K comparable, V, R any](f func(K, *V) R) Operator[K, V, R]
MapRefWithIndex returns a function that transforms each key-value pair in a map using the provided function with value references
func MapWithIndex ¶
func MapWithIndex[K comparable, V, R any](f func(K, V) R) Operator[K, V, R]
MapWithIndex returns a function that transforms each key-value pair in a map using the provided function
func Merge ¶
func Merge[K comparable, V any](right Record[K, V]) Operator[K, V, V]
Merge combines two maps giving the values in the right one precedence. Also refer to MergeMonoid
func UpsertAt ¶
func UpsertAt[K comparable, V any](k K, v V) Operator[K, V, V]
UpsertAt returns a function that inserts or updates a key-value pair in a map
type OperatorWithIndex ¶ added in v2.0.1
type OperatorWithIndex[K comparable, V1, V2 any] = func(func(K, V1) V2) Operator[K, V1, V2]
OperatorWithIndex transforms a record using both key and value information. Useful when the transformation depends on the key.
Example:
prefixWithKey := MapWithIndex(func(k string, v string) string {
return k + ":" + v
})
type Option ¶ added in v2.0.2
Option represents an optional value that may or may not be present.
func MonadLookup ¶
func MonadLookup[V any, K comparable](m Record[K, V], k K) Option[V]
MonadLookup returns the entry for a key in a map if it exists
type Predicate ¶ added in v2.0.1
Predicate is a function that tests whether a key satisfies a condition. Used in filtering operations to determine which entries to keep.
Example:
isVowel := func(k string) bool {
return strings.ContainsAny(k, "aeiou")
}
type PredicateWithIndex ¶ added in v2.0.1
type PredicateWithIndex[K comparable, V any] = func(K, V) bool
PredicateWithIndex is a function that tests whether a key-value pair satisfies a condition. Used in filtering operations that need access to both key and value.
Example:
isAdult := func(name string, user User) bool {
return user.Age >= 18
}
type Record ¶ added in v2.0.1
type Record[K comparable, V any] = map[K]V
Record represents a map with comparable keys and values of any type. This is the primary data structure for the record package, providing functional operations over Go's native map type.
Example:
type UserRecord = Record[string, User]
users := UserRecord{
"alice": User{Name: "Alice", Age: 30},
"bob": User{Name: "Bob", Age: 25},
}
func Copy ¶
func Copy[K comparable, V any](m Record[K, V]) Record[K, V]
Copy creates a shallow copy of the map
func Do ¶
func Do[K comparable, S any]() Record[K, S]
Do creates an empty context of type [S] to be used with the Bind operation. This is the starting point for do-notation style composition.
Example:
type State struct {
Name string
Count int
}
result := record.Do[string, State]()
func FromEntries ¶
func FromEntries[K comparable, V any](fa Entries[K, V]) Record[K, V]
FromEntries creates a map from an array of key-value pairs
func MonadAp ¶
func MonadAp[A any, K comparable, B any](m Monoid[Record[K, B]], fab Record[K, func(A) B], fa Record[K, A]) Record[K, B]
MonadAp applies a map of functions to a map of values, combining results using the provided Monoid
func MonadChain ¶
func MonadChain[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]], r Record[K, V1], f Kleisli[K, V1, V2]) Record[K, V2]
MonadChain chains a map transformation function that produces maps, combining results using the provided Monoid
func MonadChainWithIndex ¶
func MonadChainWithIndex[V1 any, K comparable, V2 any](m Monoid[Record[K, V2]], r Record[K, V1], f KleisliWithIndex[K, V1, V2]) Record[K, V2]
MonadChainWithIndex chains a map transformation function that produces maps, combining results using the provided Monoid
func MonadFlap ¶
func MonadFlap[B any, K comparable, A any](fab Record[K, func(A) B], a A) Record[K, B]
MonadFlap applies a value to a map of functions, producing a map of results
func MonadMap ¶
func MonadMap[K comparable, V, R any](r Record[K, V], f func(V) R) Record[K, R]
MonadMap transforms each value in a map using the provided function
func MonadMapRef ¶
func MonadMapRef[K comparable, V, R any](r Record[K, V], f func(*V) R) Record[K, R]
MonadMapRef transforms each value in a map using the provided function with value references
func MonadMapRefWithIndex ¶
func MonadMapRefWithIndex[K comparable, V, R any](r Record[K, V], f func(K, *V) R) Record[K, R]
MonadMapRefWithIndex transforms each key-value pair in a map using the provided function with value references
func MonadMapWithIndex ¶
func MonadMapWithIndex[K comparable, V, R any](r Record[K, V], f func(K, V) R) Record[K, R]
MonadMapWithIndex transforms each key-value pair in a map using the provided function
func Of ¶ added in v2.0.1
func Of[K comparable, A any](k K, a A) Record[K, A]
Of creates a map with a single key-value pair
func Singleton ¶
func Singleton[K comparable, V any](k K, v V) Record[K, V]
Singleton creates a new map with a single entry
type Reducer ¶ added in v2.0.1
type Reducer[V, R any] = func(R, V) R
Reducer accumulates values from a record into a single result. The function receives the accumulator and current value, returning the new accumulator.
Example:
sum := Reduce(func(acc int, v int) int {
return acc + v
}, 0)
type ReducerWithIndex ¶ added in v2.0.1
type ReducerWithIndex[K comparable, V, R any] = func(K, R, V) R
ReducerWithIndex accumulates values using both key and value information.
Example:
weightedSum := ReduceWithIndex(func(k string, acc int, v int) int {
weight := len(k)
return acc + (v * weight)
}, 0)
type Semigroup ¶ added in v2.0.2
Semigroup represents a semigroup structure with an associative binary operation.
func UnionFirstSemigroup ¶
func UnionFirstSemigroup[K comparable, V any]() Semigroup[Record[K, V]]
UnionFirstSemigroup creates a semigroup for maps where the first (left) value wins when the same key exists in both maps being concatenated.
This is useful when you want to preserve original values and ignore updates for keys that already exist.
When concatenating two maps:
- Keys that exist in only one map are included in the result
- Keys that exist in both maps keep the value from the first (left) map
Example:
semigroup := UnionFirstSemigroup[string, int]()
map1 := map[string]int{"a": 1, "b": 2}
map2 := map[string]int{"b": 3, "c": 4}
result := semigroup.Concat(map1, map2)
// result: {"a": 1, "b": 2, "c": 4} // b keeps value from map1 (first wins)
This is useful for:
- Default values (defaults are set first, user values don't override)
- Caching (first cached value is kept, subsequent updates ignored)
- Immutable registries (first registration wins, duplicates are ignored)
func UnionLastSemigroup ¶
func UnionLastSemigroup[K comparable, V any]() Semigroup[Record[K, V]]
UnionLastSemigroup creates a semigroup for maps where the last (right) value wins when the same key exists in both maps being concatenated.
This is the most common conflict resolution strategy and is equivalent to using the standard map merge operation where right-side values take precedence.
When concatenating two maps:
- Keys that exist in only one map are included in the result
- Keys that exist in both maps take the value from the second (right) map
Example:
semigroup := UnionLastSemigroup[string, int]()
map1 := map[string]int{"a": 1, "b": 2}
map2 := map[string]int{"b": 3, "c": 4}
result := semigroup.Concat(map1, map2)
// result: {"a": 1, "b": 3, "c": 4} // b takes value from map2 (last wins)
This is useful for:
- Configuration overrides (later configs override earlier ones)
- Applying updates to a base map
- Merging user preferences where newer values should win
func UnionSemigroup ¶
func UnionSemigroup[K comparable, V any](s Semigroup[V]) Semigroup[Record[K, V]]
UnionSemigroup creates a semigroup for maps that combines two maps using the provided semigroup for resolving conflicts when the same key exists in both maps.
When concatenating two maps:
- Keys that exist in only one map are included in the result
- Keys that exist in both maps have their values combined using the provided semigroup
This is useful when you want custom conflict resolution logic beyond simple "first wins" or "last wins" semantics.
Example:
// Create a semigroup that sums values for duplicate keys
sumSemigroup := number.SemigroupSum[int]()
mapSemigroup := UnionSemigroup[string, int](sumSemigroup)
map1 := map[string]int{"a": 1, "b": 2}
map2 := map[string]int{"b": 3, "c": 4}
result := mapSemigroup.Concat(map1, map2)
// result: {"a": 1, "b": 5, "c": 4} // b values are summed: 2 + 3 = 5
Example with string concatenation:
stringSemigroup := string.Semigroup
mapSemigroup := UnionSemigroup[string, string](stringSemigroup)
map1 := map[string]string{"a": "Hello", "b": "World"}
map2 := map[string]string{"b": "!", "c": "Goodbye"}
result := mapSemigroup.Concat(map1, map2)
// result: {"a": "Hello", "b": "World!", "c": "Goodbye"}