iter

package
v0.0.0-...-bc49051 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any[T any](l []T, predicate func(T) bool) (output bool)

Any returns if a given predicate fires for any element of a list.

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

func ApplyError[T, V any](values []T, fn func(T) (V, error)) (output []V, err error)

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 CSV

func CSV[T any](working string, value T) string

CSV folds a slice into a comma delimited list.

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 Filter

func Filter[T any](l []T, predicate func(T) bool) (output []T)

Filter returns a filtered version of the list.

func FilterRemoved

func FilterRemoved[T any](values []T, fn func(T) bool) (kept, removed []T)

FilterRemoved filters a list of values returning both the kept and removed lists.

func First

func First[T any](values []T) (output T)

First returns the first element of a slice.

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 InsertAt

func InsertAt[A any](working []A, index int, v A) (output []A)

InsertAt inserts an element into a given array at a given index.

func InsertSorted

func InsertSorted[A cmp.Ordered](working []A, v A) []A

InsertSorted performs an insertion at the index that would satisfy that the resulting array would be sorted ascending.

func InsertSortedBy

func InsertSortedBy[V any, K cmp.Ordered](working []V, v V, fn func(V) K) []V

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 Lines

func Lines[T any](working string, value T) string

Lines folds a slice into a newline delimited list.

func MergeMany

func MergeMany[T any](inputs [][]T) []T

MergeMany merges a slice of slices into a single, very large slice.

func None

func None[T any](l []T, predicate func(T) bool) (output bool)

None returns if a predicate does not fire for any element of a list.

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

func RandomSelect[A any](r *rand.Rand, values []A, count int) (output []A)

RandomSelect returns a random selection from the values.

func RandomShuffle

func RandomShuffle[A any](r *rand.Rand, values []A)

RandomShuffle shuffles the given values in place.

func Reverse

func Reverse[T any](values []T)

Reverse reverses a list 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.

func TSV

func TSV[T any](working string, value T) string

TSV folds a slice into a tab delimited list.

Types

type SorterComparer

type SorterComparer[T any] interface {
	Compare(i, j T) int
}

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

type SorterComparerFunc[T any] func(T, T) int

SorterComparer is a predicate for comparing two elements.

func (SorterComparerFunc[T]) Compare

func (scf SorterComparerFunc[T]) Compare(i, j T) int

Compare implements SorterComparer.

Jump to

Keyboard shortcuts

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