Documentation
¶
Index ¶
- func Any[AS ~[]A, PRED ~func(A) bool, A any](pred PRED) func(AS) bool
- func AnyWithIndex[AS ~[]A, PRED ~func(int, A) bool, A any](pred PRED) func(AS) bool
- func Ap[BS ~[]B, ABS ~[]func(A) B, AS ~[]A, B, A any](fa AS) func(ABS) BS
- func ApS[GS1 ~[]S1, GS2 ~[]S2, GT ~[]T, S1, S2, T any](setter func(T) func(S1) S2, fa GT) func(GS1) GS2
- func Append[GA ~[]A, A any](as GA, a A) GA
- func Bind[GS1 ~[]S1, GS2 ~[]S2, GT ~[]T, S1, S2, T any](setter func(T) func(S1) S2, f func(S1) GT) func(GS1) GS2
- func BindTo[GS1 ~[]S1, GT ~[]T, S1, T any](setter func(T) S1) func(GT) GS1
- func Chain[AS ~[]A, BS ~[]B, A, B any](f func(A) BS) func(AS) BS
- func Clone[AS ~[]A, A any](f func(A) A) func(as AS) AS
- func Copy[AS ~[]A, A any](b AS) AS
- func Do[GS ~[]S, S any](empty S) GS
- func Empty[GA ~[]A, A any]() GA
- func Extend[GA ~[]A, GB ~[]B, A, B any](f func(GA) B) func(GA) GB
- func Extract[GA ~[]A, A any](as GA) A
- func Filter[AS ~[]A, PRED ~func(A) bool, A any](pred PRED) func(AS) AS
- func FilterChain[GA ~[]A, GB ~[]B, A, B any](f func(a A) O.Option[GB]) func(GA) GB
- func FilterMap[GA ~[]A, GB ~[]B, A, B any](f func(A) O.Option[B]) func(GA) GB
- func FilterMapWithIndex[GA ~[]A, GB ~[]B, A, B any](f func(int, A) O.Option[B]) func(GA) GB
- func FilterWithIndex[AS ~[]A, PRED ~func(int, A) bool, A any](pred PRED) func(AS) AS
- func FindFirst[AS ~[]A, PRED ~func(A) bool, A any](pred PRED) func(AS) O.Option[A]
- func FindFirstMap[AS ~[]A, PRED ~func(A) O.Option[B], A, B any](pred PRED) func(AS) O.Option[B]
- func FindFirstMapWithIndex[AS ~[]A, PRED ~func(int, A) O.Option[B], A, B any](pred PRED) func(AS) O.Option[B]
- func FindFirstWithIndex[AS ~[]A, PRED ~func(int, A) bool, A any](pred PRED) func(AS) O.Option[A]
- func FindLast[AS ~[]A, PRED ~func(A) bool, A any](pred PRED) func(AS) O.Option[A]
- func FindLastMap[AS ~[]A, PRED ~func(A) O.Option[B], A, B any](pred PRED) func(AS) O.Option[B]
- func FindLastMapWithIndex[AS ~[]A, PRED ~func(int, A) O.Option[B], A, B any](pred PRED) func(AS) O.Option[B]
- func FindLastWithIndex[AS ~[]A, PRED ~func(int, A) bool, A any](pred PRED) func(AS) O.Option[A]
- func First[GA ~[]A, A any](as GA) O.Option[A]
- func Flap[FAB ~func(A) B, GFAB ~[]FAB, GB ~[]B, A, B any](a A) func(GFAB) GB
- func Flatten[GAA ~[]GA, GA ~[]A, A any](mma GAA) GA
- func Fold[AS ~[]A, A any](m M.Monoid[A]) func(AS) A
- func FoldMap[AS ~[]A, A, B any](m M.Monoid[B]) func(func(A) B) func(AS) B
- func FoldMapWithIndex[AS ~[]A, A, B any](m M.Monoid[B]) func(func(int, A) B) func(AS) B
- func From[GA ~[]A, A any](data ...A) GA
- func Head[GA ~[]A, A any](as GA) O.Option[A]
- func IsEmpty[AS ~[]A, A any](as AS) bool
- func IsNil[GA ~[]A, A any](as GA) bool
- func IsNonNil[GA ~[]A, A any](as GA) bool
- func Last[GA ~[]A, A any](as GA) O.Option[A]
- func Let[GS1 ~[]S1, GS2 ~[]S2, S1, S2, T any](key func(T) func(S1) S2, f func(S1) T) func(GS1) GS2
- func LetTo[GS1 ~[]S1, GS2 ~[]S2, S1, S2, B any](key func(B) func(S1) S2, b B) func(GS1) GS2
- func Lookup[GA ~[]A, A any](idx int) func(GA) O.Option[A]
- func MakeBy[AS ~[]A, F ~func(int) A, A any](n int, f F) AS
- func Map[GA ~[]A, GB ~[]B, A, B any](f func(a A) B) func(GA) GB
- func MapWithIndex[GA ~[]A, GB ~[]B, A, B any](f func(int, A) B) func(GA) GB
- func Match[AS ~[]A, A, B any](onEmpty func() B, onNonEmpty func(AS) B) func(AS) B
- func MatchLeft[AS ~[]A, A, B any](onEmpty func() B, onNonEmpty func(A, AS) B) func(AS) B
- func Monad[A, B any, GA ~[]A, GB ~[]B, GAB ~[]func(A) B]() monad.Monad[A, B, GA, GB, GAB]
- func MonadAp[BS ~[]B, ABS ~[]func(A) B, AS ~[]A, B, A any](fab ABS, fa AS) BS
- func MonadChain[AS ~[]A, BS ~[]B, A, B any](fa AS, f func(a A) BS) BS
- func MonadFilterMap[GA ~[]A, GB ~[]B, A, B any](fa GA, f func(A) O.Option[B]) GB
- func MonadFilterMapWithIndex[GA ~[]A, GB ~[]B, A, B any](fa GA, f func(int, A) O.Option[B]) GB
- func MonadFlap[FAB ~func(A) B, GFAB ~[]FAB, GB ~[]B, A, B any](fab GFAB, a A) GB
- func MonadMap[GA ~[]A, GB ~[]B, A, B any](as GA, f func(a A) B) GB
- func MonadMapWithIndex[GA ~[]A, GB ~[]B, A, B any](as GA, f func(int, A) B) GB
- func MonadPartition[GA ~[]A, A any](as GA, pred func(A) bool) tuple.Tuple2[GA, GA]
- func MonadReduce[GA ~[]A, A, B any](fa GA, f func(B, A) B, initial B) B
- func MonadReduceRight[GA ~[]A, A, B any](fa GA, f func(A, B) B, initial B) B
- func MonadReduceRightWithIndex[GA ~[]A, A, B any](fa GA, f func(int, A, B) B, initial B) B
- func MonadReduceWithIndex[GA ~[]A, A, B any](fa GA, f func(int, B, A) B, initial B) B
- func Monoid[GT ~[]T, T any]() M.Monoid[GT]
- func Of[GA ~[]A, A any](value A) GA
- func Partition[GA ~[]A, A any](pred func(A) bool) func(GA) tuple.Tuple2[GA, GA]
- func Prepend[ENDO ~func(AS) AS, AS []A, A any](head A) ENDO
- func Push[ENDO ~func(GA) GA, GA ~[]A, A any](a A) ENDO
- func Reduce[GA ~[]A, A, B any](f func(B, A) B, initial B) func(GA) B
- func ReduceRight[GA ~[]A, A, B any](f func(A, B) B, initial B) func(GA) B
- func ReduceRightWithIndex[GA ~[]A, A, B any](f func(int, A, B) B, initial B) func(GA) B
- func ReduceWithIndex[GA ~[]A, A, B any](f func(int, B, A) B, initial B) func(GA) B
- func Replicate[AS ~[]A, A any](n int, a A) AS
- func Reverse[GT ~[]T, T any](as GT) GT
- func Semigroup[GT ~[]T, T any]() S.Semigroup[GT]
- func Size[GA ~[]A, A any](as GA) int
- func Slice[AS ~[]A, A any](start, end int) func(AS) AS
- func SliceRight[AS ~[]A, A any](start int) func(AS) AS
- func Sort[GA ~[]T, T any](ord O.Ord[T]) func(ma GA) GA
- func SortBy[GA ~[]T, GO ~[]O.Ord[T], T any](ord GO) func(ma GA) GA
- func SortByKey[GA ~[]T, K, T any](ord O.Ord[K], f func(T) K) func(ma GA) GA
- func StrictUniq[AS ~[]A, A comparable](as AS) AS
- func Tail[GA ~[]A, A any](as GA) O.Option[GA]
- func Uniq[AS ~[]A, PRED ~func(A) K, A any, K comparable](f PRED) func(as AS) AS
- func Unzip[AS ~[]A, BS ~[]B, CS ~[]T.Tuple2[A, B], A, B any](cs CS) T.Tuple2[AS, BS]
- func UpsertAt[GA ~[]A, A any](a A) func(GA) GA
- func Zip[AS ~[]A, BS ~[]B, CS ~[]T.Tuple2[A, B], A, B any](fb BS) func(AS) CS
- func ZipWith[AS ~[]A, BS ~[]B, CS ~[]C, FCT ~func(A, B) C, A, B, C any](fa AS, fb BS, f FCT) CS
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnyWithIndex ¶
AnyWithIndex tests if any of the elements in the array matches the predicate
func ApS ¶
func ApS[GS1 ~[]S1, GS2 ~[]S2, GT ~[]T, S1, S2, T any]( 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 arrays, this produces the cartesian product.
Example:
type State struct {
X int
Y string
}
// These operations are independent and can be combined with ApS
xValues := []int{1, 2}
yValues := []string{"a", "b"}
result := F.Pipe2(
generic.Do[[]State, State](State{}),
generic.ApS[[]State, []State, []int, State, State, int](
func(x int) func(State) State {
return func(s State) State { s.X = x; return s }
},
xValues,
),
generic.ApS[[]State, []State, []string, State, State, string](
func(y string) func(State) State {
return func(s State) State { s.Y = y; return s }
},
yValues,
),
) // [{1,"a"}, {1,"b"}, {2,"a"}, {2,"b"}]
func Bind ¶
func Bind[GS1 ~[]S1, GS2 ~[]S2, GT ~[]T, S1, S2, T any]( 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 arrays, this produces the cartesian product 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 {
X int
Y int
}
result := F.Pipe2(
generic.Do[[]State, State](State{}),
generic.Bind[[]State, []State, []int, State, State, int](
func(x int) func(State) State {
return func(s State) State { s.X = x; return s }
},
func(s State) []int {
return []int{1, 2, 3}
},
),
generic.Bind[[]State, []State, []int, State, State, int](
func(y int) func(State) State {
return func(s State) State { s.Y = y; return s }
},
func(s State) []int {
// This can access s.X from the previous step
return []int{s.X * 10, s.X * 20}
},
),
) // Produces: {1,10}, {1,20}, {2,20}, {2,40}, {3,30}, {3,60}
func BindTo ¶
func BindTo[GS1 ~[]S1, GT ~[]T, S1, T any]( setter func(T) S1, ) func(GT) GS1
BindTo initializes a new state [S1] from a value [T]
func Do ¶
func Do[GS ~[]S, S any]( empty S, ) 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 {
X int
Y int
}
result := generic.Do[[]State, State](State{})
func Extend ¶ added in v2.1.7
func Extend[GA ~[]A, GB ~[]B, A, B any](f func(GA) B) func(GA) GB
Extend applies a function to every suffix of an array, creating a new array of results. This is the comonad extend operation for arrays.
The function f is applied to progressively smaller suffixes of the input array:
- f(as[0:]) for the first element
- f(as[1:]) for the second element
- f(as[2:]) for the third element
- and so on...
Type Parameters:
- GA: The input array type constraint
- GB: The output array type constraint
- A: The type of elements in the input array
- B: The type of elements in the output array
Parameters:
- f: A function that takes an array suffix and returns a value
Returns:
- A function that transforms an array of A into an array of B
Behavior:
- Creates a new array with the same length as the input
- For each position i, applies f to the suffix starting at i
- Returns an empty array if the input is empty
Example:
// Sum all elements from current position to end
sumSuffix := Extend[[]int, []int](func(as []int) int {
return MonadReduce(as, func(acc, x int) int { return acc + x }, 0)
})
result := sumSuffix([]int{1, 2, 3, 4})
// result: []int{10, 9, 7, 4}
// Explanation: [1+2+3+4, 2+3+4, 3+4, 4]
Example with length:
// Get remaining length at each position
lengths := Extend[[]int, []int](Size[[]int, int])
result := lengths([]int{10, 20, 30})
// result: []int{3, 2, 1}
Comonad laws:
- Left identity: Extend(Extract) == Identity
- Right identity: Extract ∘ Extend(f) == f
- Associativity: Extend(f) ∘ Extend(g) == Extend(f ∘ Extend(g))
func Extract ¶ added in v2.1.7
func Extract[GA ~[]A, A any](as GA) A
Extract returns the first element of an array, or a zero value if empty. This is the comonad extract operation for arrays.
Extract is the dual of the monadic return/of operation. While Of wraps a value in a context, Extract unwraps a value from its context.
Type Parameters:
- GA: The array type constraint
- A: The type of elements in the array
Parameters:
- as: The input array
Returns:
- The first element if the array is non-empty, otherwise the zero value of type A
Behavior:
- Returns as[0] if the array has at least one element
- Returns the zero value of A if the array is empty
- Does not modify the input array
Example:
result := Extract([]int{1, 2, 3})
// result: 1
Example with empty array:
result := Extract([]int{})
// result: 0 (zero value for int)
Comonad laws:
- Extract ∘ Of == Identity (extracting from a singleton returns the value)
- Extract ∘ Extend(f) == f (extract after extend equals applying f)
func FilterChain ¶
func FilterMapWithIndex ¶
func FilterWithIndex ¶
func FindFirst ¶
FindFirst finds the first element which satisfies a predicate (or a refinement) function
func FindFirstMap ¶
FindFirstMap finds the first element returned by an [O.Option] based selector function
func FindFirstMapWithIndex ¶
func FindFirstMapWithIndex[AS ~[]A, PRED ~func(int, A) O.Option[B], A, B any](pred PRED) func(AS) O.Option[B]
FindFirstMapWithIndex finds the first element returned by an [O.Option] based selector function
func FindFirstWithIndex ¶
FindFirstWithIndex finds the first element which satisfies a predicate (or a refinement) function
func FindLast ¶
FindLast finds the first element which satisfies a predicate (or a refinement) function
func FindLastMap ¶
FindLastMap finds the first element returned by an [O.Option] based selector function
func FindLastMapWithIndex ¶
func FindLastMapWithIndex[AS ~[]A, PRED ~func(int, A) O.Option[B], A, B any](pred PRED) func(AS) O.Option[B]
FindLastMapWithIndex finds the first element returned by an [O.Option] based selector function
func FindLastWithIndex ¶
FindLastWithIndex finds the first element which satisfies a predicate (or a refinement) function
func FoldMapWithIndex ¶
func From ¶
func From[GA ~[]A, A any](data ...A) GA
From constructs an array from a set of variadic arguments
func Let ¶
func Let[GS1 ~[]S1, GS2 ~[]S2, 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 ~[]S1, GS2 ~[]S2, 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 MapWithIndex ¶
func MatchLeft ¶
func MatchLeft[AS ~[]A, A, B any](onEmpty func() B, onNonEmpty func(A, AS) B) func(AS) B
func MonadChain ¶
func MonadChain[AS ~[]A, BS ~[]B, A, B any](fa AS, f func(a A) BS) BS
func MonadFilterMap ¶
func MonadFilterMapWithIndex ¶
func MonadMapWithIndex ¶
func MonadPartition ¶
func MonadReduce ¶
func MonadReduce[GA ~[]A, A, B any](fa GA, f func(B, A) B, initial B) B
func MonadReduceRight ¶
func MonadReduceRight[GA ~[]A, A, B any](fa GA, f func(A, B) B, initial B) B
func MonadReduceWithIndex ¶
func Monoid ¶
Monoid returns a Monoid instance for arrays. The Monoid combines arrays through concatenation, with an empty array as the identity element.
Example:
m := array.Monoid[int]()
result := m.Concat([]int{1, 2}, []int{3, 4}) // [1, 2, 3, 4]
empty := m.Empty() // []
func ReduceRight ¶
func ReduceRight[GA ~[]A, A, B any](f func(A, B) B, initial B) func(GA) B
func ReduceRightWithIndex ¶
func ReduceWithIndex ¶
func Semigroup ¶
Semigroup returns a Semigroup instance for arrays. The Semigroup combines arrays through concatenation.
Example:
s := array.Semigroup[int]()
result := s.Concat([]int{1, 2}, []int{3, 4}) // [1, 2, 3, 4]
func SliceRight ¶
func SortByKey ¶
SortByKey implements a stable sort on the array given the provided ordering on an extracted key
func StrictUniq ¶
func StrictUniq[AS ~[]A, A comparable](as AS) AS
StrictUniq converts an array of arbitrary items into an array or unique items where uniqueness is determined by the built-in uniqueness constraint
func Uniq ¶
func Uniq[AS ~[]A, PRED ~func(A) K, A any, K comparable](f PRED) func(as AS) AS
Uniq converts an array of arbitrary items into an array or unique items where uniqueness is determined based on a key extractor function
func Unzip ¶
Unzip is the function is reverse of Zip. Takes an array of pairs and return two corresponding arrays
func Zip ¶
Zip takes two arrays and returns an array of corresponding pairs. If one input array is short, excess elements of the longer array are discarded
func ZipWith ¶
func ZipWith[AS ~[]A, BS ~[]B, CS ~[]C, FCT ~func(A, B) C, A, B, C any](fa AS, fb BS, f FCT) CS
ZipWith applies a function to pairs of elements at the same index in two arrays, collecting the results in a new array. If one input array is short, excess elements of the longer array are discarded.
Types ¶
This section is empty.