Documentation ¶
Index ¶
- func Any[T any](l []T, predicate func(T) bool) (output bool)
- func AppendUnique[T comparable](values []T, newValue T) []T
- func Apply[T, V any](values []T, fn func(T) V) (output []V)
- func ApplyError[T, V any](values []T, fn func(T) (V, error)) (output []V, err error)
- func ApplyMap[K comparable, V, T any](values map[K]V, fn func(K, V) T) (output []T)
- func ApplyMapError[K comparable, V, T any](values map[K]V, fn func(K, V) (T, error)) (output []T, err error)
- func Argsort[A any](values []A, sortComparers ...SorterComparer[A]) []int
- func CSV[T any](working string, value T) string
- func CopyReverse[T any](values []T) []T
- func Equal[T comparable](v0, v1 []T) bool
- func Filter[T any](l []T, predicate func(T) bool) (output []T)
- func FilterRemoved[T any](values []T, fn func(T) bool) (kept, removed []T)
- func First[T any](values []T) (output T)
- func Fold[T, V any](values []T, fn func(V, T) V) (output V)
- func InsertAt[A any](working []A, index int, v A) (output []A)
- func InsertSorted[A cmp.Ordered](working []A, v A) []A
- func InsertSortedBy[V any, K cmp.Ordered](working []V, v V, fn func(V) K) []V
- func Lines[T any](working string, value T) string
- func MergeMany[T any](inputs [][]T) []T
- func None[T any](l []T, predicate func(T) bool) (output bool)
- func Permutations[A any](values ...A) [][]A
- func Powerset[A any](values ...A) (output [][]A)
- func RandomSelect[A any](r *rand.Rand, values []A, count int) (output []A)
- func RandomShuffle[A any](r *rand.Rand, values []A)
- func Reverse[T any](values []T)
- func Sort[T any](elems []T, comparers ...SorterComparer[T])
- func SorterComparerChain[T any](elems []T, comparers ...SorterComparer[T]) func(int, int) bool
- func TSV[T any](working string, value T) string
- type SorterComparer
- type SorterComparerFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendUnique ¶
func AppendUnique[T comparable](values []T, newValue T) []T
AppendUnique appends a new value to a given slice of values only if the value is _not_ currently present in the slice.
func Apply ¶
func Apply[T, V any](values []T, fn func(T) V) (output []V)
Apply maps a given function against a given input values yielding potentially a new list of a different type but of the same length.
func ApplyError ¶
ApplyError maps a given function against a given input values yielding potentially a new list of a different type but of the same length.
func ApplyMap ¶
func ApplyMap[K comparable, V, T any](values map[K]V, fn func(K, V) T) (output []T)
ApplyMap iterates over a map, calling a given function with the key and values returned from the map range, yielding an array of returns from that function.
func ApplyMapError ¶
func ApplyMapError[K comparable, V, T any](values map[K]V, fn func(K, V) (T, error)) (output []T, err error)
ApplyMapError iterates over a map, calling a given function with the key and values returned from the map range, yielding an array of returns from that function, and exiting early if the given function retruns an error.
Note that `ApplyMapError` will return any results in output already collected on error.
func Argsort ¶
func Argsort[A any](values []A, sortComparers ...SorterComparer[A]) []int
Argsort yields the sorted _indices_ of a given input as an output.
For example, if you have an array:
var nums = iter.Argsort([]int{4,3,5,2,1}, iter.SortAsc[int]()) => []int{4, 3, 1, 0, 2}
The `Argsort` can be useful in situations where you want to reference an existing slice in it's original order in some situations, e.g. in added order or similar, but in sorted order in other situations without having to copy the original slice.
func CopyReverse ¶
func CopyReverse[T any](values []T) []T
CopyReverse reverses a list and returns a new copy of the list.
func Equal ¶
func Equal[T comparable](v0, v1 []T) bool
Equal returns if two slices are elementwise comparison equal.
func FilterRemoved ¶
FilterRemoved filters a list of values returning both the kept and removed lists.
func Fold ¶
func Fold[T, V any](values []T, fn func(V, T) V) (output V)
Fold applies a function to each value in a slice, providing the current "accumulated" value across all the elements.
You can use `Fold` to merge a slice into a single value, e.g.
csv := Fold([]string{"foo","bar","baz"}, func(v0, v string) string { if v0 == "" { return v } return v0 + ", " + v }) // csv == "foo,bar,baz"
func InsertSorted ¶
InsertSorted performs an insertion at the index that would satisfy that the resulting array would be sorted ascending.
func InsertSortedBy ¶
InsertSortedBy performs an insertion at the index that would satisfy that the resulting array would be sorted ascending, provided a given value extractor for the elements.
func MergeMany ¶
func MergeMany[T any](inputs [][]T) []T
MergeMany merges a slice of slices into a single, very large slice.
func Permutations ¶
func Permutations[A any](values ...A) [][]A
Permuatations returns same length permutations of a given list of values.
For example:
['a','b','c'] => [['a','b','c'], ['a','c','b'], ['b','a','c'],
['b','c','a'], ['c','b','a'], ['c','a','b']]
func Powerset ¶
func Powerset[A any](values ...A) (output [][]A)
Powerset returns all possible selections from a given set of values with their order preserved.
func RandomSelect ¶
RandomSelect returns a random selection from the values.
func RandomShuffle ¶
RandomShuffle shuffles the given values in place.
func Sort ¶
func Sort[T any](elems []T, comparers ...SorterComparer[T])
Sort sorts a list of elements by a given list of comparers.
This can be used to sort a list of elements that may have multiple fields we want to sort by, specifically for cases where the initial fields yield equal values, e.g. if you want to sort by Year then by Name.
func SorterComparerChain ¶
func SorterComparerChain[T any](elems []T, comparers ...SorterComparer[T]) func(int, int) bool
SorterComparerChain yields a slice sort function that does a nested comparison based on a list of SorterComparers for the type.
Types ¶
type SorterComparer ¶
SorterComparer is a specific field or component of a multi-level sort.
func SortAsc ¶
func SortAsc[T cmp.Ordered]() SorterComparer[T]
SortAsc is an identity sort comparer ascending.
func SortDesc ¶
func SortDesc[T cmp.Ordered]() SorterComparer[T]
SortDesc is an identity sort descending.
func SortKeyAsc ¶
func SortKeyAsc[T any, V cmp.Ordered](fn func(T) V) SorterComparer[T]
SortKeyAsc is a sort comparer that extracts a key and sorts by it ascending.
func SortKeyDesc ¶
func SortKeyDesc[T any, V cmp.Ordered](fn func(T) V) SorterComparer[T]
SortKeyDesc is a sort comparer that extracts a key and sorts by it descending.
type SorterComparerFunc ¶
SorterComparer is a predicate for comparing two elements.
func (SorterComparerFunc[T]) Compare ¶
func (scf SorterComparerFunc[T]) Compare(i, j T) int
Compare implements SorterComparer.