Documentation ¶
Index ¶
- func Cons[T any](head T, tail []T) []T
- func FlatMap[T, U any](slc []T, fn func(t T) []U) []U
- func Head[T any](slc []T, empty func() T) (T, error)
- func Map[T any, U any](slc []T, fn func(i uint, t T) (U, error)) ([]U, error)
- func ParMap[T any, U any](ctx context.Context, slc []T, fn func(context.Context, uint, T) (U, error)) ([]U, error)
- func Reduce[ValT, AccumT any](slc []ValT, start AccumT, fn func(AccumT, ValT) AccumT) AccumT
- func Tail[T any](slc []T) ([]T, error)
- func Zip[T any](slc1 []T, slc2 []T) []T
- type Nonempty
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cons ¶
func Cons[T any](head T, tail []T) []T
Cons creates a new list with head at the front of tail
func Head ¶
Head returns slc[0] if the list has at least one element in it. Otherwise, returns empty() and a descriptive, non-nil error
func Map ¶
Map iterates through slc and, for each element, calls fn with its index and the element itself. if fn returns a non-nil error, Map returns immediately with (nil, <the_error>). Otherwise, Map assigns the first return value to a new slice at the same index and moves on. If all calls to fn return nil errors, the final slice will be returned along with a nil error
Example usage of this function:
slc := []int{1, 2, 3, 4, 5} Iter(len(slc), func(i uint) error { slc[i] += 1 })
func ParMap ¶
func ParMap[T any, U any]( ctx context.Context, slc []T, fn func(context.Context, uint, T) (U, error), ) ([]U, error)
ParMap is similar to Map, except calls fn in a separate goroutine for each element in slc. If any one of the calls to fn returns an error, the first that returns an error will have that error returned, and nil will be returned for the slice. fn will be passed a context that is derived from the input ctx.
Common use of this function is to do operations on a slice that can be done concurrently. Often this applies to "embarassingly parallel" problems.
Example usage:
var mut sync.Mutex slc := []int{1, 2, 3, 4, 5} IterPar(context.Background(), slc, func(_ context.Context, _ uint, val int) (string, error) { return strconv.Itoa(val), nil })
func Reduce ¶
func Reduce[ValT, AccumT any]( slc []ValT, start AccumT, fn func(AccumT, ValT) AccumT, ) AccumT
Reduce reduces slc down to a single element by using fn to build up an accumulator from each value in the list, starting with start.
For example, with a list of [1, 2, 3] a start value of "0", and a function that converts each integer in the list to its string representation and appends it to the end of the accumulated string, the execution of this function would look like:
accumulator := "0", index = 0, element = 1 new accumulator = "0" + "1" = "01" accumulator = "01", index = 1, element = 2 new accumulator = "01" + "2" = "012" accumulator = "012", index = 2, element = 3 new accumulator = "012" + "3" = "0123" final result = "0123"
func Tail ¶
Tail returns all but the first element of slc. If slc has less than 2 elements in it, returns nil and a non-nil, descriptive error.
func Zip ¶
func Zip[T any](slc1 []T, slc2 []T) []T
Zip combines slc1 and slc2 together. Starting at index 0, every odd number index in the returned slice will be the next successive element in slc1, and even number will be the next in slc2.
If either slice is longer than the other, the remainder of the returned slice will have the rest of the elements in the longer slice
Types ¶
type Nonempty ¶
type Nonempty[T any] struct { // contains filtered or unexported fields }
Nonempty is a list that is guaranteed to always have at least 1 element
func NewNonempty ¶
Newnonempty creates a new Nonempty list with the given head and optional tail
func (*Nonempty[T]) Append ¶
func (n *Nonempty[T]) Append(vals ...T)
Append appends vals to the end of n
func (*Nonempty[T]) Head ¶
func (n *Nonempty[T]) Head() T
Head returns the head of n. Due to the invariant that the list is non-empty, this function always returns a valid value.