Documentation ¶
Overview ¶
Package slice is a lightweight package of functions for transforming standard in-memory Go structures.
This package leans into the terminology and patterns from "seq" package, but differs in a few key areas:
- All functions are eager, i.e. executed where they are used. Nothing is lazy.
- Designed to do one-time, and one-to-one, transformations
- There is no error handling. It is designed to work with in-memory transformations, not IO or other external resources.
If you need to do more complex data transformation, like filtering out subsets of elements, handling errors, or any longer chain of transformations, you will be better off by using seq.Seq.
Index ¶
- func Copy[T any](slice []T) []T
- func Dict[I constraints.Integer, T comparable](words []T, startCorpus []T, startDict map[T]I, wordIndexBuf []I) (corpus []T, dict map[T]I, indexedWords []I)
- func FromMap[K comparable, V, T any](m map[K]V, f func(K, V) T) []T
- func Gen[T any](sz int, generator func(i int) T) []T
- func GroupBy[K comparable, V any](values []V, key func(V) K) map[K][]V
- func Keys[K comparable, V any](m map[K]V) []K
- func Mapping[S, T any](slice []S, f func(s S) T) []T
- func MappingIndex[S, T any](slice []S, f func(i int, s S) T) []T
- func Reduce[E any, R any](collector func(R, E) R, result R, from []E) R
- func SortAsc[T constraints.Ordered](slice []T) []T
- func SortDesc[T constraints.Ordered](slice []T) []T
- func ToMap[K comparable, V, T any](slice []T, f func(T) (K, V)) map[K]V
- func Uniq[T comparable](slice []T) []T
- func Values[K comparable, V any](m map[K]V) []V
- func Zero[T any](slice []T) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dict ¶
func Dict[I constraints.Integer, T comparable](words []T, startCorpus []T, startDict map[T]I, wordIndexBuf []I) (corpus []T, dict map[T]I, indexedWords []I)
Dict is versatile function for deduplicating values, composing indexes, and converting values into integer offsets into a corpus. It looks at a slice of "words" and builds a corpus of unique words, and a dictionary mapping single words to offsets in the corpus.
You can build up state with repeated calls by passing in pre-populated start-corpus and dictionary. Most often from results of previous calls to Dict.
If wordIndexBuf is non-nil it will be used to build the indexedWords that maps the "words" argument to the corpus. If wordIndexBuf is too small a new buffer will be allocated. If wordIndexBuf is nil then the returned indexedWords will also be nil.
Example 1: Building a Corpus of Unique Words ¶
words := []string{"one", "two", "one", "three"} corpus, dict, _ := Dict(words, nil, map[string]uint16{}, nil) // corpus is now: // []string{"one", "two", "three"} // dict maps word indexes in the corpus: // map[string]uint16{"one": 0, "two": 1, "three": 2} // We can further build our corpus and dict: moreWords := []string{"four", "one"} corpus, dict, _ = Dict(moreWords, corpus, dict, nil) // corpus is now: // []string{"one", "two", "three", "four"} // dict maps word indexes in the corpus: // map[string]uint16{"one": 0, "two": 1, "three": 2, "four": 3}
Example 2: Representing Words as Offsets Reduce A Corpus ¶
// With the 'corpus' and 'dict' variables from Example 1 lastWords := []string{"one", "two", "five"} indexedWords := []uint16{} // create a non-nil buffer for the indexed words corpus, dict, indexedWords = Dict(lastWords, corpus, dict, indexedWords) // indexedWords is now: // []uint16{0, 1, 4} // which corresponds exactly with the word-offsets in the updated corpus: // []string{"one", "two", "three", "four", "five"}
func FromMap ¶
func FromMap[K comparable, V, T any](m map[K]V, f func(K, V) T) []T
FromMap builds a slice []T from a map[K]V. To build a map from a slice use ToMap. Please bear in mind that go maps are unordered and the resulting slice will reflect that.
func Gen ¶
Gen builds a new slice of a given size. The generator function is called for each index in the new slice.
func GroupBy ¶
func GroupBy[K comparable, V any](values []V, key func(V) K) map[K][]V
GroupBy returns a map where elements with the same key are collected together in a slice.
func Keys ¶
func Keys[K comparable, V any](m map[K]V) []K
Keys returns a slice with all keys from a map.
func Mapping ¶
func Mapping[S, T any](slice []S, f func(s S) T) []T
Mapping converts a slice from one type to another
func MappingIndex ¶
MappingIndex converts a slice from one type to another. The mapping function also receives the index of the element being transformed.
func Reduce ¶
Reduce collects a slice of elements E into a result of type R. This operation is also known as "fold" in other frameworks.
It is a simplified form of seq.Reduce found in the core package, and works with standard seq.FuncCollect functions like seq.MakeString, seq.MakeBytes, seq.Count, seq.MakeSet, fnmath.Min, fnmath.Max, fnmath.Sum etc.
The first argument "result" is the initial state. The second is the collector function. The signature of the collector works like standard append(). The last argument is the slice to collect values from.
Example: ¶
Summing the elements in and integer slice could look like:
vals := []int{1, 2, 3} sum := Reduce(0, func(res, e int) int { return res + e }, vals) // sum is now: 6
func SortAsc ¶
func SortAsc[T constraints.Ordered](slice []T) []T
SortAsc sorts a slice in-place. The argument is returned to facilitate easy chaining.
func SortDesc ¶
func SortDesc[T constraints.Ordered](slice []T) []T
SortDesc sorts a slice in-place. The argument is returned to facilitate easy chaining.
func ToMap ¶
func ToMap[K comparable, V, T any](slice []T, f func(T) (K, V)) map[K]V
ToMap builds a map[K]V from a slice. To build a slice from a map use FromMap. The mnemonic for the function name is that the last word is what you operate on.
func Uniq ¶
func Uniq[T comparable](slice []T) []T
Uniq returns a slice with all the unique values from the input slice, in the order they appear.
func Values ¶
func Values[K comparable, V any](m map[K]V) []V
Values returns a slice with all keys from a map.
Types ¶
This section is empty.