package array

import "github.com/IBM/fp-go/array"

Example (Any)

Code:

{

	pred := func(val int) bool {
		return val&2 == 0
	}

	data1 := From(1, 2, 3)

	fmt.Println(Any(pred)(data1))

	// Output:
	// true
}

Output:

true
Example (Any_filter)

Code:

{

	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
}

Output:

true
Example (Any_find)

Code:

{

	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
}

Output:

true
Example (Basic)

Example_basic adapts examples from [https://github.com/inato/fp-ts-cheatsheet#basic-manipulation]

Code:

{

	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]
}

Output:

[0 4 16 36 64]
[0 4 16 36 64]
Example (Find)

Code:

{

	pred := func(val int) bool {
		return val&2 == 0
	}

	data1 := From(1, 2, 3)

	fmt.Println(FindFirst(pred)(data1))

	// Output:
	// Some[int](1)
}

Output:

Some[int](1)
Example (Find_filter)

Code:

{

	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)
}

Output:

Some[int](1)
Example (Sort)

Example_sort adapts examples from [https://github.com/inato/fp-ts-cheatsheet#sort-elements-with-ord]

Code:play 

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

Examples

Functions

func Any

func Any[A any](pred func(A) bool) func([]A) bool

Any tests if any of the elements in the array matches the predicate

func AnyWithIndex

func AnyWithIndex[A any](pred func(int, A) bool) func([]A) bool

AnyWithIndex tests if any of the elements in the array matches the predicate

func Ap

func Ap[B, A any](fa []A) func([]func(A) B) []B

func ApS

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 Append

func Append[A any](as []A, a A) []A

func ArrayConcatAll

func ArrayConcatAll[A any](data ...[]A) []A

ConcatAll efficiently concatenates the input arrays into a final array

func ArrayOption

func ArrayOption[A any]() func([]O.Option[A]) O.Option[[]A]

ArrayOption returns a function to convert sequence of options into an option of a sequence

func Bind

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

func BindTo[S1, T any](
	setter func(T) S1,
) func([]T) []S1

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

func Chain

func Chain[A, B any](f func(A) []B) func([]A) []B

func Clone

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 ConcatAll

func ConcatAll[A any](m M.Monoid[A]) func([]A) A

func ConstNil

func ConstNil[A any]() []A

ConstNil returns a nil array

func Copy

func Copy[A any](b []A) []A

Copy creates a shallow copy of the array

func Do

func Do[S any](
	empty S,
) []S

Bind creates an empty context of type [S] to be used with the Bind operation

func Empty

func Empty[A any]() []A

func Eq

func Eq[T any](e E.Eq[T]) E.Eq[[]T]

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

func FilterChain[A, B any](f func(A) O.Option[[]B]) func([]A) []B

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

func FilterMap[A, B any](f func(A) O.Option[B]) func([]A) []B

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 FilterMapRef[A, B any](pred func(a *A) bool, f func(a *A) B) func([]A) []B

func FilterMapWithIndex

func FilterMapWithIndex[A, B any](f func(int, A) O.Option[B]) func([]A) []B

FilterMapWithIndex maps an array with an iterating function that returns an [O.Option] and it keeps only the Some values discarding the Nones.

func FilterRef

func FilterRef[A any](pred func(*A) bool) EM.Endomorphism[[]A]

func FilterWithIndex

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

func FindFirst[A any](pred func(A) bool) func([]A) O.Option[A]

FindFirst finds the first element which satisfies a predicate (or a refinement) function

func FindFirstMap

func FindFirstMap[A, B any](sel func(A) O.Option[B]) func([]A) O.Option[B]

FindFirstMap finds the first element returned by an [O.Option] based selector function

func FindFirstMapWithIndex

func FindFirstMapWithIndex[A, B any](sel func(int, A) O.Option[B]) func([]A) O.Option[B]

FindFirstMapWithIndex finds the first element returned by an [O.Option] based selector function

func FindFirstWithIndex

func FindFirstWithIndex[A any](pred func(int, A) bool) func([]A) O.Option[A]

FindFirstWithIndex finds the first element which satisfies a predicate (or a refinement) function

func FindLast

func FindLast[A any](pred func(A) bool) func([]A) O.Option[A]

FindLast finds the Last element which satisfies a predicate (or a refinement) function

func FindLastMap

func FindLastMap[A, B any](sel func(A) O.Option[B]) func([]A) O.Option[B]

FindLastMap finds the Last element returned by an [O.Option] based selector function

func FindLastMapWithIndex

func FindLastMapWithIndex[A, B any](sel func(int, A) O.Option[B]) func([]A) O.Option[B]

FindLastMapWithIndex finds the Last element returned by an [O.Option] based selector function

func FindLastWithIndex

func FindLastWithIndex[A any](pred func(int, A) bool) func([]A) O.Option[A]

FindLastWithIndex finds the Last element which satisfies a predicate (or a refinement) function

func First

func First[A any](as []A) O.Option[A]

func Flap

func Flap[B, A any](a A) func([]func(A) B) []B

func Flatten

func Flatten[A any](mma [][]A) []A

func Fold

func Fold[A any](m M.Monoid[A]) func([]A) A

Fold folds the array using the provided Monoid.

func FoldMap

func FoldMap[A, B any](m M.Monoid[B]) func(func(A) B) func([]A) B

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

Code:

{
	src := From("a", "b", "c")

	fold := FoldMap[string](S.Monoid)(strings.ToUpper)

	fmt.Println(fold(src))

	// Output: ABC

}

Output:

ABC

func FoldMapWithIndex

func FoldMapWithIndex[A, B any](m M.Monoid[B]) func(func(int, A) B) func([]A) B

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 Head[A any](as []A) O.Option[A]

func Intercalate

func Intercalate[A any](m M.Monoid[A]) func(A) func([]A) A

func Intersperse

func Intersperse[A any](middle A) EM.Endomorphism[[]A]

func IsEmpty

func IsEmpty[A any](as []A) bool

func IsNil

func IsNil[A any](as []A) bool

IsNil checks if the array is set to nil

func IsNonEmpty

func IsNonEmpty[A any](as []A) bool

func IsNonNil

func IsNonNil[A any](as []A) bool

IsNonNil checks if the array is set to nil

func Last

func Last[A any](as []A) O.Option[A]

func Let

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

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 Lookup

func Lookup[A any](idx int) func([]A) O.Option[A]

func MakeBy

func MakeBy[F ~func(int) A, A any](n int, f F) []A

MakeBy returns a `Array` of length `n` with element `i` initialized with `f(i)`.

func Map

func Map[A, B any](f func(a A) B) func([]A) []B

func MapRef

func MapRef[A, B any](f func(a *A) B) func([]A) []B

func MapWithIndex

func MapWithIndex[A, B any](f func(int, A) B) func([]A) []B

func Match

func Match[A, B any](onEmpty func() B, onNonEmpty func([]A) B) func([]A) B

func MatchLeft

func MatchLeft[A, B any](onEmpty func() B, onNonEmpty func(A, []A) B) func([]A) B

func Monad

func Monad[A, B any]() monad.Monad[A, B, []A, []B, []func(A) B]

Monad returns the monadic operations for an array

func MonadAp

func MonadAp[B, A any](fab []func(A) B, fa []A) []B

func MonadChain

func MonadChain[A, B any](fa []A, f func(a A) []B) []B

func MonadFilterMap

func MonadFilterMap[A, B any](fa []A, f func(A) O.Option[B]) []B

func MonadFilterMapWithIndex

func MonadFilterMapWithIndex[A, B any](fa []A, f func(int, A) O.Option[B]) []B

func MonadFlap

func MonadFlap[B, A any](fab []func(A) B, a A) []B

func MonadMap

func MonadMap[A, B any](as []A, f func(a A) B) []B

func MonadMapRef

func MonadMapRef[A, B any](as []A, f func(a *A) B) []B

func MonadPartition

func MonadPartition[A any](as []A, pred func(A) bool) tuple.Tuple2[[]A, []A]

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 Monoid

func Monoid[T any]() M.Monoid[[]T]

func Of

func Of[A any](a A) []A

Of constructs a single element array

func Partition

func Partition[A any](pred func(A) bool) func([]A) tuple.Tuple2[[]A, []A]

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

func Prepend[A any](head A) EM.Endomorphism[[]A]

func PrependAll

func PrependAll[A any](middle A) EM.Endomorphism[[]A]

func Push

func Push[A any](a A) EM.Endomorphism[[]A]

func Reduce

func Reduce[A, B any](f func(B, A) B, initial B) func([]A) B

func ReduceRef

func ReduceRef[A, B any](f func(B, *A) B, initial B) func([]A) B

func ReduceRight

func ReduceRight[A, B any](f func(A, B) B, initial B) func([]A) B

func ReduceRightWithIndex

func ReduceRightWithIndex[A, B any](f func(int, A, B) B, initial B) func([]A) B

func ReduceWithIndex

func ReduceWithIndex[A, B any](f func(int, B, A) B, initial B) func([]A) B

func Replicate

func Replicate[A any](n int, a A) []A

Replicate creates a `Array` containing a value repeated the specified number of times.

func Semigroup

func Semigroup[T any]() S.Semigroup[[]T]

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 Size

func Size[A any](as []A) int

func Slice

func Slice[A any](low, high int) func(as []A) []A

func SliceRight

func SliceRight[A any](start int) EM.Endomorphism[[]A]

func Sort

func Sort[T any](ord O.Ord[T]) func(ma []T) []T

Sort implements a stable sort on the array given the provided ordering

func SortBy

func SortBy[T any](ord []O.Ord[T]) func(ma []T) []T

SortBy implements a stable sort on the array given the provided ordering

func SortByKey

func SortByKey[K, T any](ord O.Ord[K], f func(T) K) func(ma []T) []T

SortByKey implements a stable sort on the array given the provided ordering on an extracted key

func StrictUniq

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 Tail

func Tail[A any](as []A) O.Option[[]A]

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

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

func Unzip[A, B any](cs []T.Tuple2[A, B]) T.Tuple2[[]A, []B]

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 Zero

func Zero[A any]() []A

func Zip

func Zip[A, B any](fb []B) func([]A) []T.Tuple2[A, B]

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[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.

Source Files

any.go array.go bind.go eq.go find.go magma.go monad.go monoid.go sequence.go sort.go traverse.go uniq.go zip.go

Directories

PathSynopsis
array/generic
array/nonempty
array/testing
Version
v1.0.151 (latest)
Published
Nov 23, 2024
Platform
linux/amd64
Imports
11 packages
Last checked
4 months ago

Tools for package owners.