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 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 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 := slice.Reduce(func(res, e int) int { return res + e }, 0, 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.
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.