Documentation
¶
Overview ¶
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 (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 Ap[B, A any](fa []A) func([]func(A) B) []B
- 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 Chain[A, B any](f func(A) []B) func([]A) []B
- func ConcatAll[A any](m M.Monoid[A]) func([]A) A
- func ConstNil[A any]() []A
- func Copy[A any](b []A) []A
- func Empty[A any]() []A
- func Eq[T any](e E.Eq[T]) E.Eq[[]T]
- func Filter[A any](pred func(A) bool) func([]A) []A
- func FilterChain[A, B any](f func(A) O.Option[[]B]) func([]A) []B
- func FilterMap[A, B any](f func(a 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 FilterRef[A any](pred func(*A) bool) func([]A) []A
- func First[A any](as []A) O.Option[A]
- func Flap[A, B 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 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) func([]A) []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 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 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 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 A) O.Option[B]) []B
- func MonadFlap[A, B 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 PrependAll[A any](middle A) func([]A) []A
- func Push[A any](a A) func([]A) []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 Replicate[A any](n int, a A) []A
- 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) func([]A) []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 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 Unzip[A, B any](cs []T.Tuple2[A, B]) T.Tuple2[[]A, []B]
- func UpsertAt[A any](a A) func([]A) []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 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 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 ¶
FilterChain 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 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 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) func([]A) []A
func IsNonEmpty ¶
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 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 PrependAll ¶
func PrependAll[A any](middle A) func([]A) []A
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 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 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 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 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.