Documentation ¶
Overview ¶
Example (Any) ¶
pred := func(val int) bool { return val&2 == 0 } data1 := From(1, 2, 3) fmt.Println(Any(pred)(data1))
Output: true
Example (Any_filter) ¶
pred := func(val int) bool { return val&2 == 0 } data1 := From(1, 2, 3) // Any tests if any of the entries in the array matches the condition Any := F.Flow2( Filter(pred), IsNonEmpty[int], ) fmt.Println(Any(data1))
Output: true
Example (Any_find) ¶
pred := func(val int) bool { return val&2 == 0 } data1 := From(1, 2, 3) // Any tests if any of the entries in the array matches the condition Any := F.Flow2( FindFirst(pred), O.IsSome[int], ) fmt.Println(Any(data1))
Output: true
Example (Basic) ¶
Example_basic adapts examples from [https://github.com/inato/fp-ts-cheatsheet#basic-manipulation]
someArray := From(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) // []int isEven := func(num int) bool { return num%2 == 0 } square := func(num int) int { return num * num } // filter and map result := F.Pipe2( someArray, Filter(isEven), Map(square), ) // [0 4 16 36 64] // or in one go with filterMap resultFilterMap := F.Pipe1( someArray, FilterMap( F.Flow2(O.FromPredicate(isEven), O.Map(square)), ), ) fmt.Println(result) fmt.Println(resultFilterMap)
Output: [0 4 16 36 64] [0 4 16 36 64]
Example (Find) ¶
pred := func(val int) bool { return val&2 == 0 } data1 := From(1, 2, 3) fmt.Println(FindFirst(pred)(data1))
Output: Some[int](1)
Example (Find_filter) ¶
pred := func(val int) bool { return val&2 == 0 } data1 := From(1, 2, 3) Find := F.Flow2( Filter(pred), Head[int], ) fmt.Println(Find(data1))
Output: Some[int](1)
Example (Sort) ¶
Example_sort adapts examples from [https://github.com/inato/fp-ts-cheatsheet#sort-elements-with-ord]
package main import ( "fmt" F "github.com/IBM/fp-go/function" I "github.com/IBM/fp-go/number/integer" O "github.com/IBM/fp-go/option" "github.com/IBM/fp-go/ord" S "github.com/IBM/fp-go/string" ) type user struct { name string age O.Option[int] } func (user user) GetName() string { return user.name } func (user user) GetAge() O.Option[int] { return user.age } // Example_sort adapts examples from [https://github.com/inato/fp-ts-cheatsheet#sort-elements-with-ord] func main() { strings := From("zyx", "abc", "klm") sortedStrings := F.Pipe1( strings, Sort(S.Ord), ) // => ['abc', 'klm', 'zyx'] // reverse sort reverseSortedStrings := F.Pipe1( strings, Sort(ord.Reverse(S.Ord)), ) // => ['zyx', 'klm', 'abc'] // sort Option optionalNumbers := From(O.Some(1337), O.None[int](), O.Some(42)) sortedNums := F.Pipe1( optionalNumbers, Sort(O.Ord(I.Ord)), ) // complex object with different rules byName := F.Pipe1( S.Ord, ord.Contramap(user.GetName), ) // ord.Ord[user] byAge := F.Pipe1( O.Ord(I.Ord), ord.Contramap(user.GetAge), ) // ord.Ord[user] sortedUsers := F.Pipe1( From(user{name: "a", age: O.Of(30)}, user{name: "d", age: O.Of(10)}, user{name: "c"}, user{name: "b", age: O.Of(10)}), SortBy(From(byAge, byName)), ) fmt.Println(sortedStrings) fmt.Println(reverseSortedStrings) fmt.Println(sortedNums) fmt.Println(sortedUsers) }
Output: [abc klm zyx] [zyx klm abc] [None[int] Some[int](42) Some[int](1337)] [{c {false 0}} {b {true 10}} {d {true 10}} {a {true 30}}]
Index ¶
- func Any[A any](pred func(A) bool) func([]A) bool
- func AnyWithIndex[A any](pred func(int, A) bool) func([]A) bool
- func Ap[B, A any](fa []A) func([]func(A) B) []B
- func ApS[S1, S2, T any](setter func(T) func(S1) S2, fa []T) func([]S1) []S2
- func Append[A any](as []A, a A) []A
- func ArrayConcatAll[A any](data ...[]A) []A
- func ArrayOption[A any]() func([]O.Option[A]) O.Option[[]A]
- func Bind[S1, S2, T any](setter func(T) func(S1) S2, f func(S1) []T) func([]S1) []S2
- func BindTo[S1, T any](setter func(T) S1) func([]T) []S1
- func Chain[A, B any](f func(A) []B) func([]A) []B
- func Clone[A any](f func(A) A) func(as []A) []A
- func ConcatAll[A any](m M.Monoid[A]) func([]A) A
- func ConstNil[A any]() []A
- func Copy[A any](b []A) []A
- func Do[S any](empty S) []S
- func Empty[A any]() []A
- func Eq[T any](e E.Eq[T]) E.Eq[[]T]
- func Filter[A any](pred func(A) bool) EM.Endomorphism[[]A]
- func FilterChain[A, B any](f func(A) O.Option[[]B]) func([]A) []B
- func FilterMap[A, B any](f func(A) O.Option[B]) func([]A) []B
- func FilterMapRef[A, B any](pred func(a *A) bool, f func(a *A) B) func([]A) []B
- func FilterMapWithIndex[A, B any](f func(int, A) O.Option[B]) func([]A) []B
- func FilterRef[A any](pred func(*A) bool) EM.Endomorphism[[]A]
- func FilterWithIndex[A any](pred func(int, A) bool) EM.Endomorphism[[]A]
- func FindFirst[A any](pred func(A) bool) func([]A) O.Option[A]
- func FindFirstMap[A, B any](sel func(A) O.Option[B]) func([]A) O.Option[B]
- func FindFirstMapWithIndex[A, B any](sel func(int, A) O.Option[B]) func([]A) O.Option[B]
- func FindFirstWithIndex[A any](pred func(int, A) bool) func([]A) O.Option[A]
- func FindLast[A any](pred func(A) bool) func([]A) O.Option[A]
- func FindLastMap[A, B any](sel func(A) O.Option[B]) func([]A) O.Option[B]
- func FindLastMapWithIndex[A, B any](sel func(int, A) O.Option[B]) func([]A) O.Option[B]
- func FindLastWithIndex[A any](pred func(int, A) bool) func([]A) O.Option[A]
- func First[A any](as []A) O.Option[A]
- func Flap[B, A any](a A) func([]func(A) B) []B
- func Flatten[A any](mma [][]A) []A
- func Fold[A any](m M.Monoid[A]) func([]A) A
- func FoldMap[A, B any](m M.Monoid[B]) func(func(A) B) func([]A) B
- func FoldMapWithIndex[A, B any](m M.Monoid[B]) func(func(int, A) B) func([]A) B
- func From[A any](data ...A) []A
- func Head[A any](as []A) O.Option[A]
- func Intercalate[A any](m M.Monoid[A]) func(A) func([]A) A
- func Intersperse[A any](middle A) EM.Endomorphism[[]A]
- func IsEmpty[A any](as []A) bool
- func IsNil[A any](as []A) bool
- func IsNonEmpty[A any](as []A) bool
- func IsNonNil[A any](as []A) bool
- func Last[A any](as []A) O.Option[A]
- func Let[S1, S2, T any](setter func(T) func(S1) S2, f func(S1) T) func([]S1) []S2
- func LetTo[S1, S2, T any](setter func(T) func(S1) S2, b T) func([]S1) []S2
- func Lookup[A any](idx int) func([]A) O.Option[A]
- func MakeBy[F ~func(int) A, A any](n int, f F) []A
- func Map[A, B any](f func(a A) B) func([]A) []B
- func MapRef[A, B any](f func(a *A) B) func([]A) []B
- func MapWithIndex[A, B any](f func(int, A) B) func([]A) []B
- func Match[A, B any](onEmpty func() B, onNonEmpty func([]A) B) func([]A) B
- func MatchLeft[A, B any](onEmpty func() B, onNonEmpty func(A, []A) B) func([]A) B
- func Monad[A, B any]() monad.Monad[A, B, []A, []B, []func(A) B]
- func MonadAp[B, A any](fab []func(A) B, fa []A) []B
- func MonadChain[A, B any](fa []A, f func(a A) []B) []B
- func MonadFilterMap[A, B any](fa []A, f func(A) O.Option[B]) []B
- func MonadFilterMapWithIndex[A, B any](fa []A, f func(int, A) O.Option[B]) []B
- func MonadFlap[B, A any](fab []func(A) B, a A) []B
- func MonadMap[A, B any](as []A, f func(a A) B) []B
- func MonadMapRef[A, B any](as []A, f func(a *A) B) []B
- func MonadPartition[A any](as []A, pred func(A) bool) tuple.Tuple2[[]A, []A]
- func MonadTraverse[A, B, HKTB, HKTAB, HKTRB any](fof func([]B) HKTRB, fmap func(func([]B) func(B) []B) func(HKTRB) HKTAB, ...) HKTRB
- func Monoid[T any]() M.Monoid[[]T]
- func Of[A any](a A) []A
- func Partition[A any](pred func(A) bool) func([]A) tuple.Tuple2[[]A, []A]
- func Prepend[A any](head A) EM.Endomorphism[[]A]
- func PrependAll[A any](middle A) EM.Endomorphism[[]A]
- func Push[A any](a A) EM.Endomorphism[[]A]
- func Reduce[A, B any](f func(B, A) B, initial B) func([]A) B
- func ReduceRef[A, B any](f func(B, *A) B, initial B) func([]A) B
- func ReduceRight[A, B any](f func(A, B) B, initial B) func([]A) B
- func ReduceRightWithIndex[A, B any](f func(int, A, B) B, initial B) func([]A) B
- func ReduceWithIndex[A, B any](f func(int, B, A) B, initial B) func([]A) B
- func Replicate[A any](n int, a A) []A
- func Semigroup[T any]() S.Semigroup[[]T]
- func Sequence[A, HKTA, HKTRA, HKTFRA any](_of func([]A) HKTRA, _map func(HKTRA, func([]A) func(A) []A) HKTFRA, ...) func([]HKTA) HKTRA
- func Size[A any](as []A) int
- func Slice[A any](low, high int) func(as []A) []A
- func SliceRight[A any](start int) EM.Endomorphism[[]A]
- func Sort[T any](ord O.Ord[T]) func(ma []T) []T
- func SortBy[T any](ord []O.Ord[T]) func(ma []T) []T
- func SortByKey[K, T any](ord O.Ord[K], f func(T) K) func(ma []T) []T
- func StrictUniq[A comparable](as []A) []A
- func Tail[A any](as []A) O.Option[[]A]
- func Traverse[A, B, HKTB, HKTAB, HKTRB any](fof func([]B) HKTRB, fmap func(func([]B) func(B) []B) func(HKTRB) HKTAB, ...) func([]A) HKTRB
- func Uniq[A any, K comparable](f func(A) K) func(as []A) []A
- func Unzip[A, B any](cs []T.Tuple2[A, B]) T.Tuple2[[]A, []B]
- func UpsertAt[A any](a A) EM.Endomorphism[[]A]
- func Zero[A any]() []A
- func Zip[A, B any](fb []B) func([]A) []T.Tuple2[A, B]
- func ZipWith[FCT ~func(A, B) C, A, B, C any](fa []A, fb []B, f FCT) []C
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnyWithIndex ¶ added in v1.0.56
AnyWithIndex tests if any of the elements in the array matches the predicate
func ApS ¶ added in v1.0.107
func ApS[S1, S2, T any]( setter func(T) func(S1) S2, fa []T, ) func([]S1) []S2
ApS attaches a value to a context [S1] to produce a context [S2] by considering the context and the value concurrently
func ArrayConcatAll ¶
func ArrayConcatAll[A any](data ...[]A) []A
ConcatAll efficiently concatenates the input arrays into a final array
func ArrayOption ¶
ArrayOption returns a function to convert sequence of options into an option of a sequence
func Bind ¶ added in v1.0.107
func Bind[S1, S2, T any]( setter func(T) func(S1) S2, f func(S1) []T, ) func([]S1) []S2
Bind attaches the result of a computation to a context [S1] to produce a context [S2]
func BindTo ¶ added in v1.0.107
func BindTo[S1, T any]( setter func(T) S1, ) func([]T) []S1
BindTo initializes a new state [S1] from a value [T]
func Clone ¶ added in v1.0.75
func Clone[A any](f func(A) A) func(as []A) []A
Clone creates a deep copy of the array using the provided endomorphism to clone the values
func Do ¶ added in v1.0.107
func Do[S any]( empty S, ) []S
Bind creates an empty context of type [S] to be used with the Bind operation
func Filter ¶
func Filter[A any](pred func(A) bool) EM.Endomorphism[[]A]
Filter returns a new array with all elements from the original array that match a predicate
func FilterChain ¶ added in v1.0.15
FilterChain maps an array with an iterating function that returns an [O.Option] of an array. It keeps only the Some values discarding the Nones and then flattens the result.
func FilterMap ¶
FilterMap maps an array with an iterating function that returns an [O.Option] and it keeps only the Some values discarding the Nones.
func FilterMapRef ¶
func FilterMapWithIndex ¶ added in v1.0.56
FilterMapWithIndex maps an array with an iterating function that returns an [O.Option] and it keeps only the Some values discarding the Nones.
func FilterWithIndex ¶ added in v1.0.56
func FilterWithIndex[A any](pred func(int, A) bool) EM.Endomorphism[[]A]
FilterWithIndex returns a new array with all elements from the original array that match a predicate
func FindFirst ¶ added in v1.0.56
FindFirst finds the first element which satisfies a predicate (or a refinement) function
func FindFirstMap ¶ added in v1.0.56
FindFirstMap finds the first element returned by an [O.Option] based selector function
func FindFirstMapWithIndex ¶ added in v1.0.56
FindFirstMapWithIndex finds the first element returned by an [O.Option] based selector function
func FindFirstWithIndex ¶ added in v1.0.56
FindFirstWithIndex finds the first element which satisfies a predicate (or a refinement) function
func FindLast ¶ added in v1.0.56
FindLast finds the Last element which satisfies a predicate (or a refinement) function
func FindLastMap ¶ added in v1.0.56
FindLastMap finds the Last element returned by an [O.Option] based selector function
func FindLastMapWithIndex ¶ added in v1.0.56
FindLastMapWithIndex finds the Last element returned by an [O.Option] based selector function
func FindLastWithIndex ¶ added in v1.0.56
FindLastWithIndex finds the Last element which satisfies a predicate (or a refinement) function
func FoldMap ¶ added in v1.0.16
FoldMap maps and folds an array. Map the Array passing each value to the iterating function. Then fold the results using the provided Monoid.
Example ¶
src := From("a", "b", "c") fold := FoldMap[string](S.Monoid)(strings.ToUpper) fmt.Println(fold(src))
Output: ABC
func FoldMapWithIndex ¶ added in v1.0.58
FoldMapWithIndex maps and folds an array. Map the Array passing each value to the iterating function. Then fold the results using the provided Monoid.
func From ¶
func From[A any](data ...A) []A
From constructs an array from a set of variadic arguments
func Intercalate ¶
func Intersperse ¶
func Intersperse[A any](middle A) EM.Endomorphism[[]A]
func IsNonEmpty ¶
func Let ¶ added in v1.0.107
func Let[S1, S2, T any]( setter func(T) func(S1) S2, f func(S1) T, ) func([]S1) []S2
Let attaches the result of a computation to a context [S1] to produce a context [S2]
func LetTo ¶ added in v1.0.107
func LetTo[S1, S2, T any]( setter func(T) func(S1) S2, b T, ) func([]S1) []S2
LetTo attaches the a value to a context [S1] to produce a context [S2]
func MapWithIndex ¶ added in v1.0.58
func MatchLeft ¶ added in v1.0.6
func MatchLeft[A, B any](onEmpty func() B, onNonEmpty func(A, []A) B) func([]A) B
func MonadChain ¶
func MonadChain[A, B any](fa []A, f func(a A) []B) []B
func MonadFilterMap ¶
func MonadFilterMapWithIndex ¶ added in v1.0.56
func MonadMapRef ¶
func MonadMapRef[A, B any](as []A, f func(a *A) B) []B
func MonadTraverse ¶
func MonadTraverse[A, B, HKTB, HKTAB, HKTRB any]( fof func([]B) HKTRB, fmap func(func([]B) func(B) []B) func(HKTRB) HKTAB, fap func(HKTB) func(HKTAB) HKTRB, ta []A, f func(A) HKTB) HKTRB
func Partition ¶
Partition creates two new arrays out of one, the left result contains the elements for which the predicate returns false, the right one those for which the predicate returns true
func Prepend ¶ added in v1.0.87
func Prepend[A any](head A) EM.Endomorphism[[]A]
func PrependAll ¶
func PrependAll[A any](middle A) EM.Endomorphism[[]A]
func Push ¶ added in v1.0.20
func Push[A any](a A) EM.Endomorphism[[]A]
func ReduceRight ¶ added in v1.0.56
func ReduceRight[A, B any](f func(A, B) B, initial B) func([]A) B
func ReduceRightWithIndex ¶ added in v1.0.56
func ReduceWithIndex ¶ added in v1.0.56
func Replicate ¶
Replicate creates a `Array` containing a value repeated the specified number of times.
func Sequence ¶
func Sequence[A, HKTA, HKTRA, HKTFRA any]( _of func([]A) HKTRA, _map func(HKTRA, func([]A) func(A) []A) HKTFRA, _ap func(HKTFRA, HKTA) HKTRA, ) func([]HKTA) HKTRA
Sequence takes an `Array` where elements are `HKT<A>` (higher kinded type) and, using an applicative of that `HKT`, returns an `HKT` of `[]A`. e.g. it can turn an `[]Either[error, string]` into an `Either[error, []string]`.
Sequence requires an `Applicative` of the `HKT` you are targeting, e.g. to turn an `[]Either[E, A]` into an `Either[E, []A]`, it needs an Applicative` for `Either`, to to turn an `[]Option[A]` into an `Option[ []A]`, it needs an `Applicative` for `Option`.
func SliceRight ¶ added in v1.0.9
func SliceRight[A any](start int) EM.Endomorphism[[]A]
func SortBy ¶ added in v1.0.19
SortBy implements a stable sort on the array given the provided ordering
func SortByKey ¶ added in v1.0.16
SortByKey implements a stable sort on the array given the provided ordering on an extracted key
func StrictUniq ¶ added in v1.0.64
func StrictUniq[A comparable](as []A) []A
StrictUniq converts an array of arbitrary items into an array or unique items where uniqueness is determined by the built-in uniqueness constraint
func Traverse ¶
func Traverse[A, B, HKTB, HKTAB, HKTRB any]( fof func([]B) HKTRB, fmap func(func([]B) func(B) []B) func(HKTRB) HKTAB, fap func(HKTB) func(HKTAB) HKTRB, f func(A) HKTB) func([]A) HKTRB
func Uniq ¶ added in v1.0.64
func Uniq[A any, K comparable](f func(A) K) func(as []A) []A
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 ¶ added in v1.0.11
Unzip is the function is reverse of Zip. Takes an array of pairs and return two corresponding arrays
func UpsertAt ¶
func UpsertAt[A any](a A) EM.Endomorphism[[]A]
func Zip ¶ added in v1.0.11
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 ¶ added in v1.0.11
func ZipWith[FCT ~func(A, B) C, A, B, C any](fa []A, fb []B, f FCT) []C
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.