fn

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: MIT Imports: 1 Imported by: 5

README

go-fn CI godoc goreportcard license

Go module for functions that mimic useful JavaScript functions using Go 1.18's Generics for some functional flavor!

install

go get github.com/frantjc/go-fn

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Coalesce

func Coalesce[T comparable](ts ...T) T

func Every

func Every[T any](in []T, f func(T, int) bool) bool

Every tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

func Find

func Find[T any](in []T, f func(T, int) bool) T

Find returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, the zero value of T is returned.

func FindIndex

func FindIndex[T any](in []T, f func(T, int) bool) int

FindIndex returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

func ForEach

func ForEach[T any](in []T, f func(T, int))

ForEach executes a provided function once for each array element.

func Includes

func Includes[T comparable](in []T, a T) bool

Includes determines whether an array includes a certain value among its entries, returning true or false as appropriate.

func IndexOf

func IndexOf[T comparable](in []T, a T) int

IndexOf returns the first index at which a given element can be found in the array, or -1 if it is not present.

func Join

func Join[T any](in []T, separator string) string

Join creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

func LastIndexOf

func LastIndexOf[T comparable](in []T, a T, from int) int

LastIndexOf returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

func Ptr added in v0.3.0

func Ptr[T any](t T) *T

func Reduce

func Reduce[T1, T2 any](in []T1, f func(T2, T1, int) T2, initial T2) T2

Reduce executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The first time that the callback is run there is no "return value of the previous calculation". If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).

func ReduceRight

func ReduceRight[T1, T2 any](in []T1, f func(T2, T1, int) T2, initial T2) T2

ReduceRight applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

func Some

func Some[T any](in []T, f func(T, int) bool) bool

Some tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

func Sort

func Sort[T comparable](in []T, f func(a, b T) int) []T

Sort creates a new array by sorting the elements of the given array and returns the sorted array. The sort order is ascending.

func Ternary

func Ternary[T any](condition bool, a, b T) T

Ternary returns the first T if the conditional evaluates to true, otherwise it returns the second T.

func Unique

func Unique[T comparable](in []T) []T

Unique creates a new array with only unique elements from the given array.

Types

type AnyArray added in v0.2.0

type AnyArray[T any] []T

AnyArray is an array of any and has methods that mimic a subset of the JavaScript array functions.

func Filter

func Filter[T any](in []T, f func(T, int) bool) AnyArray[T]

Filter creates a new array with all elements that pass the test implemented by the provided function.

func Map

func Map[T1, T2 any](in []T1, f func(T1, int) T2) AnyArray[T2]

Map creates a new array populated with the results of calling a provided function on every element in the calling array.

func Reverse

func Reverse[T any](in []T) AnyArray[T]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func Slice

func Slice[T any](in []T, start, end int) AnyArray[T]

Slice returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

func (AnyArray[T]) Every added in v0.2.0

func (a AnyArray[T]) Every(f func(T, int) bool) bool

Every tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

func (AnyArray[T]) Filter added in v0.2.0

func (a AnyArray[T]) Filter(f func(T, int) bool) AnyArray[T]

Filter creates a new array with all elements that pass the test implemented by the provided function.

func (AnyArray[T]) Find added in v0.2.0

func (a AnyArray[T]) Find(f func(T, int) bool) T

Find returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, the zero value of T is returned.

func (AnyArray[T]) FindIndex added in v0.2.0

func (a AnyArray[T]) FindIndex(f func(T, int) bool) int

FindIndex returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test

func (AnyArray[T]) ForEach added in v0.2.0

func (a AnyArray[T]) ForEach(f func(T, int))

ForEach executes a provided function once for each array element

func (AnyArray[T]) Reverse added in v0.2.0

func (a AnyArray[T]) Reverse() AnyArray[T]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func (AnyArray[T]) Slice added in v0.2.0

func (a AnyArray[T]) Slice(start, end int) AnyArray[T]

Slice returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

func (AnyArray[T]) Some added in v0.2.0

func (a AnyArray[T]) Some(f func(T, int) bool) bool

Some tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

type ComparableArray added in v0.2.0

type ComparableArray[T comparable] AnyArray[T]

ComparableArray is an array of comparable and has methods that mimic a subset of the JavaScript array functions that are unique to comparables.

func (ComparableArray[T]) Filter added in v0.2.0

func (a ComparableArray[T]) Filter(f func(T, int) bool) ComparableArray[T]

Filter creates a new array with all elements that pass the test implemented by the provided function.

func (ComparableArray[T]) Includes added in v0.2.0

func (a ComparableArray[T]) Includes(b T) bool

Includes determines whether an array includes a certain value among its entries, returning true or false as appropriate.

func (ComparableArray[T]) IndexOf added in v0.2.0

func (a ComparableArray[T]) IndexOf(b T) int

IndexOf returns the first index at which a given element can be found in the array, or -1 if it is not present.

func (ComparableArray[T]) LastIndexOf added in v0.2.0

func (a ComparableArray[T]) LastIndexOf(b T, from int) int

LastIndexOf returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

func (ComparableArray[T]) Reverse added in v0.2.0

func (a ComparableArray[T]) Reverse() ComparableArray[T]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func (ComparableArray[T]) Slice added in v0.2.0

func (a ComparableArray[T]) Slice(start, end int) ComparableArray[T]

Slice returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

func (ComparableArray[T]) Unique added in v0.2.0

func (a ComparableArray[T]) Unique() ComparableArray[T]

Unique creates a new array with only unique elements from the given array.

type ReducibleArray added in v0.2.0

type ReducibleArray[T1, T2 any] AnyArray[T1]

ReducibleArray is an array of any and has methods that mimic the JavaScript array reduce functions.

T1 represents the type of the array, while T2 represents the type of the array intended to be mapped to

func (ReducibleArray[T1, T2]) Filter added in v0.2.0

func (a ReducibleArray[T1, T2]) Filter(f func(T1, int) bool) ReducibleArray[T1, T2]

Filter creates a new array with all elements that pass the test implemented by the provided function.

func (ReducibleArray[T1, T2]) Map added in v0.2.0

func (a ReducibleArray[T1, T2]) Map(f func(T1, int) T2) AnyArray[T2]

Map creates a new array populated with the results of calling a provided function on every element in the calling array.

func (ReducibleArray[T1, T2]) Reduce added in v0.2.0

func (a ReducibleArray[T1, T2]) Reduce(f func(T2, T1, int) T2, initial T2) T2

Reduce executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The first time that the callback is run there is no "return value of the previous calculation". If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).

func (ReducibleArray[T1, T2]) ReduceRight added in v0.2.0

func (a ReducibleArray[T1, T2]) ReduceRight(f func(T2, T1, int) T2, initial T2) T2

ReduceRight applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

func (ReducibleArray[T1, T2]) Reverse added in v0.2.0

func (a ReducibleArray[T1, T2]) Reverse() ReducibleArray[T1, T2]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func (ReducibleArray[T1, T2]) Slice added in v0.2.0

func (a ReducibleArray[T1, T2]) Slice(start, end int) ReducibleArray[T1, T2]

Slice returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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