slices

package
v0.0.2-pre Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any[T any](s []T, fn SearchFn[T]) bool

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

func Each[T any](ss []T, fn IterFn[T])

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

func EachErr[T any](ss []T, fn IterErrFn[T]) error

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

func Filter[T any](s []T, fn SearchFn[T]) []T

Filter returns a new slice containing only those elements which succeed for the given SearchFn.

func First

func First[T any](s []T, fn SearchFn[T]) (int, T)

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

func Map[S any, T any](ss []S, fn MapFn[S, T]) []T

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

func MapEach[S any, T any](ss []S, fn MapEachFn[S, T]) []T

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

func MapErr[S any, T any](ss []S, fn MapErrFn[S, T]) ([]T, error)

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

func Reduce[A any, T any](st []T, acc A, fn ReduceFn[A, T]) A

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

func RemoveFirst[T any](s *[]T, fn SearchFn[T]) T

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

type IterErrFn[T any] func(idx int, elem T) error

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

type IterFn[T any] func(idx int, elem T)

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

type MapEachErrFn[S any, T any] func(idx int, s S) (T, error)

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

type MapEachFn[S any, T any] func(idx int, s S) T

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

type MapErrFn[S any, T any] func(s S) (T, error)

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

type MapFn[S any, T any] func(s S) T

MapFn defines the type of function provided to Map. It passes a specific element from a slice and returns a mapped value.

type ReduceErrFn

type ReduceErrFn[A any, T any] func(acc A, val T) (A, error)

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

type ReduceFn[A any, T any] func(acc A, val T) A

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

type SearchFn[T any] func(idx int, elem T) bool

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

func NewStack[T any](elems ...T) Stack[T]

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

func (s *Stack[T]) Pop() (T, bool)

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}

Jump to

Keyboard shortcuts

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