Documentation
¶
Index ¶
- func Any[T any](s []T, fn SearchFn[T]) bool
- func Contains[T comparable](s []T, t T) bool
- func Each[T any](ss []T, fn IterFn[T])
- func EachErr[T any](ss []T, fn IterErrFn[T]) error
- func Filter[T any](s []T, fn SearchFn[T]) []T
- func First[T any](s []T, fn SearchFn[T]) (int, T)
- func Flatten[T any](ss [][]T) []T
- func Map[S any, T any](ss []S, fn MapFn[S, T]) []T
- func MapEach[S any, T any](ss []S, fn MapEachFn[S, T]) []T
- func MapEachErr[S any, T any](ss []S, fn MapEachErrFn[S, T]) ([]T, error)
- func MapErr[S any, T any](ss []S, fn MapErrFn[S, T]) ([]T, error)
- func Reduce[A any, T any](st []T, acc A, fn ReduceFn[A, T]) A
- func ReduceErr[A any, T any](st []T, acc A, fn ReduceErrFn[A, T]) (A, error)
- func Remove[T comparable](a, b []T) []T
- func RemoveFirst[T any](s *[]T, fn SearchFn[T]) T
- func Seq[T typing.OrderedNumeric](a, b T) []T
- func Sum[T typing.OrderedNumeric](s []T) T
- func Uniques[S any, T comparable](ss []S, fn MapFn[S, T]) []T
- type IterErrFn
- type IterFn
- type MapEachErrFn
- type MapEachFn
- type MapErrFn
- type MapFn
- type ReduceErrFn
- type ReduceFn
- type SearchFn
- type Stack
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Any ¶
Any returns true if at least one element in the provided slice succeeds for the SearchFn.
func Contains ¶
func Contains[T comparable](s []T, t T) bool
Contains returns true if s contains the element t. The type of T must be comparable using the `==` operator. It is effectively a specific implementation of Any.
func Each ¶
Each calls the provided function for each element in the provided slice. Use Each if errors are unexpected or safe to ignore, otherwise use EachErr.
func EachErr ¶
EachErr calls the provided function for each element in the provided slice. EachErr will halt whenever the provided function returns an error. Use EachErr if errors are meaningful, otherwise use Each.
func Filter ¶
Filter returns a new slice containing only those elements which succeed for the given SearchFn.
func First ¶
First returns the first element and its index which succeeds for the given SearchFn. If no element found, index is returned as -1 along with the zero value for the element.
func Flatten ¶
func Flatten[T any](ss [][]T) []T
Flatten takes a slice of slices of type T and returns a flattened slice of type T; all elements of each sub-slice combined into a single slice.
func Map ¶
Map provides mapping functionality over a slice using a MapFn. Use this mapper if errors are unexpected or safe to ignore, otherwise use MapErr.
func MapEach ¶
MapEach provides mapping functionality over a slice using a MapEachFn. Use this mapper if errors are unexpected or safe to ignore, otherwise use MapEachErr.
func MapEachErr ¶
func MapEachErr[S any, T any](ss []S, fn MapEachErrFn[S, T]) ([]T, error)
MapEachErr provides mapping functionality over a slice using a MapErrFn. It will halt mapping if the provided function returns an error and return the slice populated to that point along with the error. Use this mapper if errors are meaningful, otherwise use Map.
func MapErr ¶
MapErr provides mapping functionality over a slice using a MapErrFn. It will halt mapping if the provided function returns an error and return the slice populated to that point along with the error. Use this mapper if errors are meaningful, otherwise use Map.
func Reduce ¶
Reduce provides folding functionality over a slice using a ReduceFn. Use this reducer if errors are unexpected or safe to ignore, otherwise use ReduceErr.
func ReduceErr ¶
func ReduceErr[A any, T any](st []T, acc A, fn ReduceErrFn[A, T]) (A, error)
ReduceErr provides folding functionality over a slice using a ReduceErrFn. It will halt folding if the provided function returns an error and return the accumulator value at that point along with the error. Use this reducer if errors are meaningful, otherwise use Reduce.
func Remove ¶
func Remove[T comparable](a, b []T) []T
Remove returns a new slice of T containing any elements in a that are not in b. The type of T must be comparable using the `==` operator. It is effectively a specific implementation of a Filter.
func RemoveFirst ¶
First returns the first element which succeeds for the given SearchFn and removes that elements from the provided slice. NOTE: you need to pass a reference to the slice.
func Seq ¶
func Seq[T typing.OrderedNumeric](a, b T) []T
func Sum ¶
func Sum[T typing.OrderedNumeric](s []T) T
func Uniques ¶
func Uniques[S any, T comparable](ss []S, fn MapFn[S, T]) []T
Uniques returns a new slice containing unique values returned from fn.
Types ¶
type IterErrFn ¶
IterErrFn defines a function that can be passed to any of the iterative slice functions which are tolerant of errors. It provides the index of an element and an element from the iterator and return any error.
type IterFn ¶
IterFn defines a function that can be passed to any of the iterative slice functions which do not produce errors. It provides the index of an element and an element from the iterator.
type MapEachErrFn ¶
MapErrFn defines the type of function provided to MapErr. It passes a specific index and element from a slice and returns a mapped value and any error.
type MapEachFn ¶
MapEachFn defines the type of function provided to MapEach. It passes a specific index and element from a slice and returns a mapped value.
type MapErrFn ¶
MapErrFn defines the type of function provided to MapErr. It passes a specific element from a slice and returns a mapped value and any error.
type MapFn ¶
MapFn defines the type of function provided to Map. It passes a specific element from a slice and returns a mapped value.
type ReduceErrFn ¶
ReduceErrFn defines the type of function provided to ReduceErr. It passes the accumulator and a specific element from a slice and returns the accumulator and any error.
type ReduceFn ¶
ReduceFn defines the type of function provided to Reduce. It passes the accumulator and a specific element from a slice and returns the accumulator.
type SearchFn ¶
SearchFn defines a function which can be provided to any of the search-related slice functions. For each element in a slice, the search function will receive the element's index and the element itself as arguments; the function should return true or false if the element applies to the search.
type Stack ¶
type Stack[T any] []T
Stack implements a stack via a slice. Because elements pushed on this Stack are added to the head of the slice, if you were to loop/range over a Stack, elements will be retrieved in the same order as if you were popping them.
func NewStack ¶
NewStack creates a new Stack. If any elements are provided, they are treated as if they are already a stack; thus they will be popped in the order they are provided.
func (*Stack[T]) Pop ¶
Pop removes the element at the head of the stack and returns that value. If no more elements are in the stack, a zero-value is returned along with a false value as the second argument.
func (*Stack[T]) Push ¶
func (s *Stack[T]) Push(vs ...T)
Push adds one or more new elements to the head of the stack. If more than one element is provided, each is pushed to the stack individually. Example:
s := NewStack[int]() s.Push(0) # s == Stack{0} s.Push(1, 2, 3, 4) # s == Stack{4, 3, 2, 1, 0}