array

package
v1.0.148 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: Apache-2.0 Imports: 11 Imported by: 18

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any added in v1.0.56

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 added in v1.0.56

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 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 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 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 Chain

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

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 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 added in v1.0.9

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

Copy creates a shallow copy of the array

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 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 added in v1.0.15

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 added in v1.0.56

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

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 added in v1.0.56

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 added in v1.0.56

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 added in v1.0.56

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 added in v1.0.56

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 added in v1.0.56

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 added in v1.0.56

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 added in v1.0.56

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 added in v1.0.39

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

func Flatten

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

func Fold added in v1.0.16

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

Fold folds the array using the provided Monoid.

func FoldMap added in v1.0.16

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

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 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 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 added in v1.0.58

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 added in v1.0.6

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

func Monad added in v1.0.113

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 added in v1.0.56

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

func MonadFlap added in v1.0.39

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 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 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 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 ReduceRightWithIndex[A, B any](f func(int, A, B) B, initial B) func([]A) B

func ReduceWithIndex added in v1.0.56

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 added in v1.0.58

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 added in v1.0.9

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 added in v1.0.19

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 added in v1.0.16

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

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 added in v1.0.11

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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