generic

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ap

func Ap[BS ~map[K]B, ABS ~map[K]func(A) B, AS ~map[K]A, K comparable, B, A any](m Mo.Monoid[BS]) func(fa AS) func(ABS) BS

func ApS

func ApS[GS1 ~map[K]S1, GS2 ~map[K]S2, GT ~map[K]T, K comparable, S1, S2, T any](m Mo.Monoid[GS2]) func(setter func(T) func(S1) S2, fa GT) func(GS1) GS2

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. For records, this merges values by key.

Example:

type State struct {
    Name  string
    Score int
}

// These operations are independent and can be combined with ApS
names := map[string]string{"player1": "Alice", "player2": "Bob"}
scores := map[string]int{"player1": 100, "player2": 200}

result := F.Pipe2(
    generic.Do[map[string]State, string, State](),
    generic.ApS[map[string]State, map[string]State, map[string]string, string, State, State, string](
        monoid.Record[string, State](),
    )(
        func(name string) func(State) State {
            return func(s State) State { s.Name = name; return s }
        },
        names,
    ),
    generic.ApS[map[string]State, map[string]State, map[string]int, string, State, State, int](
        monoid.Record[string, State](),
    )(
        func(score int) func(State) State {
            return func(s State) State { s.Score = score; return s }
        },
        scores,
    ),
) // map[string]State{"player1": {Name: "Alice", Score: 100}, "player2": {Name: "Bob", Score: 200}}

func Bind

func Bind[GS1 ~map[K]S1, GS2 ~map[K]S2, GT ~map[K]T, K comparable, S1, S2, T any](m Mo.Monoid[GS2]) func(setter func(T) func(S1) S2, f func(S1) GT) func(GS1) GS2

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 where later steps can use values from earlier steps.

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(
    generic.Do[map[string]State, string, State](),
    generic.Bind[map[string]State, map[string]State, map[string]string, string, State, State, string](
        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"}
        },
    ),
    generic.Bind[map[string]State, map[string]State, map[string]int, string, State, State, int](
        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 BindTo

func BindTo[GS1 ~map[K]S1, GT ~map[K]T, K comparable, S1, T any](setter func(T) S1) func(GT) GS1

BindTo initializes a new state [S1] from a value [T]

func Chain

func Chain[M ~map[K]V1, N ~map[K]V2, K comparable, V1, V2 any](m Mo.Monoid[N]) func(func(V1) N) func(M) N

func ChainWithIndex

func ChainWithIndex[M ~map[K]V1, N ~map[K]V2, K comparable, V1, V2 any](m Mo.Monoid[N]) func(func(K, V1) N) func(M) N

func Clone

func Clone[M ~map[K]V, K comparable, V any](f func(V) V) func(m M) M

func Collect

func Collect[M ~map[K]V, GR ~[]R, K comparable, V, R any](f func(K, V) R) func(M) GR

func CollectOrd

func CollectOrd[M ~map[K]V, GR ~[]R, K comparable, V, R any](o ord.Ord[K]) func(f func(K, V) R) func(M) GR

func ConstNil

func ConstNil[M ~map[K]V, K comparable, V any]() M

ConstNil return a nil map

func Copy

func Copy[M ~map[K]V, K comparable, V any](m M) M

func DeleteAt

func DeleteAt[M ~map[K]V, K comparable, V any](k K) func(M) M

func Do

func Do[GS ~map[K]S, K comparable, S any]() GS

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 := generic.Do[map[string]State, string, State]()

func Empty

func Empty[M ~map[K]V, K comparable, V any]() M

func Eq

func Eq[M ~map[K]V, K comparable, V any](e E.Eq[V]) E.Eq[M]

func Filter

func Filter[M ~map[K]V, K comparable, V any](f func(K) bool) func(M) M

Filter creates a new map with only the elements that match the predicate

func FilterChain

func FilterChain[M ~map[K]V1, N ~map[K]V2, K comparable, V1, V2 any](m Mo.Monoid[N]) func(func(V1) O.Option[N]) func(M) N

FilterChain creates a new map with only the elements for which the transformation function creates a Some

func FilterChainWithIndex

func FilterChainWithIndex[M ~map[K]V1, N ~map[K]V2, K comparable, V1, V2 any](m Mo.Monoid[N]) func(func(K, V1) O.Option[N]) func(M) N

FilterChainWithIndex creates a new map with only the elements for which the transformation function creates a Some

func FilterMap

func FilterMap[M ~map[K]V1, N ~map[K]V2, K comparable, V1, V2 any](f func(V1) O.Option[V2]) func(M) N

FilterMap creates a new map with only the elements for which the transformation function creates a Some

func FilterMapWithIndex

func FilterMapWithIndex[M ~map[K]V1, N ~map[K]V2, K comparable, V1, V2 any](f func(K, V1) O.Option[V2]) func(M) N

FilterMapWithIndex creates a new map with only the elements for which the transformation function creates a Some

func FilterWithIndex

func FilterWithIndex[M ~map[K]V, K comparable, V any](f func(K, V) bool) func(M) M

FilterWithIndex creates a new map with only the elements that match the predicate

func Flap

func Flap[GFAB ~map[K]func(A) B, GB ~map[K]B, K comparable, A, B any](a A) func(GFAB) GB

func Flatten

func Flatten[M ~map[K]N, N ~map[K]V, K comparable, V any](m Mo.Monoid[N]) func(M) N

Flatten converts a nested map into a regular map

func Fold

func Fold[AS ~map[K]A, K comparable, A any](m Mo.Monoid[A]) func(AS) A

func FoldMap

func FoldMap[AS ~map[K]A, K comparable, A, B any](m Mo.Monoid[B]) func(func(A) B) func(AS) B

func FoldMapOrd

func FoldMapOrd[AS ~map[K]A, K comparable, A, B any](o ord.Ord[K]) func(m Mo.Monoid[B]) func(func(A) B) func(AS) B

func FoldMapOrdWithIndex

func FoldMapOrdWithIndex[AS ~map[K]A, K comparable, A, B any](o ord.Ord[K]) func(m Mo.Monoid[B]) func(func(K, A) B) func(AS) B

func FoldMapWithIndex

func FoldMapWithIndex[AS ~map[K]A, K comparable, A, B any](m Mo.Monoid[B]) func(func(K, A) B) func(AS) B

func FoldOrd

func FoldOrd[AS ~map[K]A, K comparable, A any](o ord.Ord[K]) func(m Mo.Monoid[A]) func(AS) A

func FromArray

func FromArray[
	GA ~[]T.Tuple2[K, V],
	M ~map[K]V,
	K comparable,
	V any](m Mg.Magma[V]) func(fa GA) M

func FromArrayMap

func FromArrayMap[
	FCT ~func(A) T.Tuple2[K, V],
	GA ~[]A,
	M ~map[K]V,
	A any,
	K comparable,
	V any](m Mg.Magma[V]) func(f FCT) func(fa GA) M

func FromEntries

func FromEntries[M ~map[K]V, GT ~[]T.Tuple2[K, V], K comparable, V any](fa GT) M

func FromFoldable

func FromFoldable[
	HKTA any,
	FOLDABLE ~func(func(M, T.Tuple2[K, V]) M, M) func(HKTA) M,
	M ~map[K]V,
	K comparable,
	V any](m Mg.Magma[V], red FOLDABLE) func(fa HKTA) M

func FromFoldableMap

func FromFoldableMap[
	FCT ~func(A) T.Tuple2[K, V],
	HKTA any,
	FOLDABLE ~func(func(M, A) M, M) func(HKTA) M,
	M ~map[K]V,
	A any,
	K comparable,
	V any](m Mg.Magma[V], fld FOLDABLE) func(f FCT) func(fa HKTA) M

FromFoldableMap uses the reduce method for a higher kinded type to transform its values into a tuple. The key and value are then used to populate the map. Duplicate values are resolved via the provided [Mg.Magma]

func FromStrictEquals

func FromStrictEquals[M ~map[K]V, K, V comparable]() E.Eq[M]

FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function

func Has

func Has[M ~map[K]V, K comparable, V any](k K, r M) bool

func IsEmpty

func IsEmpty[M ~map[K]V, K comparable, V any](r M) bool

func IsNil

func IsNil[M ~map[K]V, K comparable, V any](m M) bool

IsNil checks if the map is set to nil

func IsNonEmpty

func IsNonEmpty[M ~map[K]V, K comparable, V any](r M) bool

func IsNonNil

func IsNonNil[M ~map[K]V, K comparable, V any](m M) bool

IsNonNil checks if the map is set to nil

func Keys

func Keys[M ~map[K]V, GK ~[]K, K comparable, V any](r M) GK

func KeysOrd

func KeysOrd[M ~map[K]V, GK ~[]K, K comparable, V any](o ord.Ord[K]) func(r M) GK

func Let

func Let[GS1 ~map[K]S1, GS2 ~map[K]S2, K comparable, S1, S2, T any](
	key func(T) func(S1) S2,
	f func(S1) T,
) func(GS1) GS2

Let attaches the result of a computation to a context [S1] to produce a context [S2]

func LetTo

func LetTo[GS1 ~map[K]S1, GS2 ~map[K]S2, K comparable, S1, S2, B any](
	key func(B) func(S1) S2,
	b B,
) func(GS1) GS2

LetTo attaches the a value to a context [S1] to produce a context [S2]

func Lookup

func Lookup[M ~map[K]V, K comparable, V any](k K) func(M) O.Option[V]

func Map

func Map[M ~map[K]V, N ~map[K]R, K comparable, V, R any](f func(V) R) func(M) N

func MapRef

func MapRef[M ~map[K]V, N ~map[K]R, K comparable, V, R any](f func(*V) R) func(M) N

func MapRefWithIndex

func MapRefWithIndex[M ~map[K]V, N ~map[K]R, K comparable, V, R any](f func(K, *V) R) func(M) N

func MapWithIndex

func MapWithIndex[M ~map[K]V, N ~map[K]R, K comparable, V, R any](f func(K, V) R) func(M) N

func Merge

func Merge[M ~map[K]V, K comparable, V any](right M) func(M) M

func MonadAp

func MonadAp[BS ~map[K]B, ABS ~map[K]func(A) B, AS ~map[K]A, K comparable, B, A any](m Mo.Monoid[BS], fab ABS, fa AS) BS

func MonadChain

func MonadChain[M ~map[K]V1, N ~map[K]V2, K comparable, V1, V2 any](m Mo.Monoid[N], r M, f func(V1) N) N

func MonadChainWithIndex

func MonadChainWithIndex[M ~map[K]V1, N ~map[K]V2, K comparable, V1, V2 any](m Mo.Monoid[N], r M, f func(K, V1) N) N

func MonadFlap

func MonadFlap[GFAB ~map[K]func(A) B, GB ~map[K]B, K comparable, A, B any](fab GFAB, a A) GB

func MonadLookup

func MonadLookup[M ~map[K]V, K comparable, V any](m M, k K) O.Option[V]

func MonadMap

func MonadMap[M ~map[K]V, N ~map[K]R, K comparable, V, R any](r M, f func(V) R) N

func MonadMapRef

func MonadMapRef[M ~map[K]V, N ~map[K]R, K comparable, V, R any](r M, f func(*V) R) N

func MonadMapRefWithIndex

func MonadMapRefWithIndex[M ~map[K]V, N ~map[K]R, K comparable, V, R any](r M, f func(K, *V) R) N

func MonadMapWithIndex

func MonadMapWithIndex[M ~map[K]V, N ~map[K]R, K comparable, V, R any](r M, f func(K, V) R) N

func Reduce

func Reduce[M ~map[K]V, K comparable, V, R any](f func(R, V) R, initial R) func(M) R

func ReduceOrd

func ReduceOrd[M ~map[K]V, K comparable, V, R any](o ord.Ord[K]) func(func(R, V) R, R) func(M) R

func ReduceOrdWithIndex

func ReduceOrdWithIndex[M ~map[K]V, K comparable, V, R any](o ord.Ord[K]) func(func(K, R, V) R, R) func(M) R

func ReduceRef

func ReduceRef[M ~map[K]V, K comparable, V, R any](f func(R, *V) R, initial R) func(M) R

func ReduceRefWithIndex

func ReduceRefWithIndex[M ~map[K]V, K comparable, V, R any](f func(K, R, *V) R, initial R) func(M) R

func ReduceWithIndex

func ReduceWithIndex[M ~map[K]V, K comparable, V, R any](f func(K, R, V) R, initial R) func(M) R

func Singleton

func Singleton[M ~map[K]V, K comparable, V any](k K, v V) M

func Size

func Size[M ~map[K]V, K comparable, V any](r M) int

func ToArray

func ToArray[M ~map[K]V, GT ~[]T.Tuple2[K, V], K comparable, V any](r M) GT

func ToEntries

func ToEntries[M ~map[K]V, GT ~[]T.Tuple2[K, V], K comparable, V any](r M) GT

func ToEntriesOrd

func ToEntriesOrd[M ~map[K]V, GT ~[]T.Tuple2[K, V], K comparable, V any](o ord.Ord[K]) func(r M) GT

func Union

func Union[M ~map[K]V, K comparable, V any](m Mg.Magma[V]) func(M) func(M) M

func UnionFirst

func UnionFirst[M ~map[K]V, K comparable, V any](right M) func(M) M

func UnionFirstMonoid

func UnionFirstMonoid[N ~map[K]V, K comparable, V any]() M.Monoid[N]

func UnionFirstSemigroup

func UnionFirstSemigroup[N ~map[K]V, K comparable, V any]() S.Semigroup[N]

func UnionLast

func UnionLast[M ~map[K]V, K comparable, V any](right M) func(M) M

func UnionLastMonoid

func UnionLastMonoid[N ~map[K]V, K comparable, V any]() M.Monoid[N]

func UnionLastSemigroup

func UnionLastSemigroup[N ~map[K]V, K comparable, V any]() S.Semigroup[N]

func UnionMonoid

func UnionMonoid[N ~map[K]V, K comparable, V any](s S.Semigroup[V]) M.Monoid[N]

func UnionSemigroup

func UnionSemigroup[N ~map[K]V, K comparable, V any](s S.Semigroup[V]) S.Semigroup[N]

func UpsertAt

func UpsertAt[M ~map[K]V, K comparable, V any](k K, v V) func(M) M

func Values

func Values[M ~map[K]V, GV ~[]V, K comparable, V any](r M) GV

func ValuesOrd

func ValuesOrd[M ~map[K]V, GV ~[]V, K comparable, V any](o ord.Ord[K]) func(r M) GV

Types

This section is empty.

Jump to

Keyboard shortcuts

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