array

package
v1.0.47 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: Apache-2.0 Imports: 8 Imported by: 19

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ap

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

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 Chain

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

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

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) func([]A) []A

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 A) O.Option[B]) func([]A) []B

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

func FilterRef

func FilterRef[A any](pred func(*A) bool) func([]A) []A

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 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) func([]A) []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 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 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 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 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 PrependAll

func PrependAll[A any](middle A) func([]A) []A

func Push added in v1.0.20

func Push[A any](a A) func([]A) []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 Replicate

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

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 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) func([]A) []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 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 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) func([]A) []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