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.
Interoperability with seq.Slice ¶
It is important to note that all functions that take a []T argument can be passed a seq.Slice[T] as well. Any seq.Slice from the seq package can be passed as arguments to functions in this package (and anywhere else a slice is used).
Index ¶
- func Copy[T any](slice []T) []T
- func Delete[T any](s []T, shouldDelete func(T) bool) []T
- func Equal[T comparable](s1, s2 []T) bool
- func First[T any](s []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() T) []T
- func GenIndex[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 HasPrefix[T comparable](s, prefix []T) bool
- func HasSuffix[T comparable](s, suffix []T) bool
- func Keys[K comparable, V any](m map[K]V) []K
- func Last[T any](s []T) T
- 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 Reverse[T any](s []T) []T
- func Shuffle[T any](s []T) []T
- 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 Trim[T comparable](s []T) []T
- 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 Delete ¶ added in v0.9.0
Delete removes all slice entries where the predicate returns true. The input slice is changed in-place.
func Equal ¶ added in v0.4.0
func Equal[T comparable](s1, s2 []T) bool
Equal returns true the two slices contains the same elements. Empty slices and nil slices are considered equal.
func First ¶ added in v0.4.0
func First[T any](s []T) T
First returns the first element of s or the zero value of T
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 sz times.
Example:
tenRandomFloats := slice.Gen(10, rand.Float64)
func GenIndex ¶ added in v0.9.0
GenIndex 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 HasPrefix ¶ added in v0.4.0
func HasPrefix[T comparable](s, prefix []T) bool
HasPrefix returns true if the slice s starts with the given prefix. If the prefix is empty (including nil) this function always returns true.
func HasSuffix ¶ added in v0.4.0
func HasSuffix[T comparable](s, suffix []T) bool
HasSuffix returns true if the slice s ends with the given suffix. If the suffix is empty (including nil) this function always returns true.
func Keys ¶
func Keys[K comparable, V any](m map[K]V) []K
Keys returns a slice with all keys from a map.
func Last ¶ added in v0.4.0
func Last[T any](s []T) T
Last returns the last element of s or the zero value of T
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 Reverse ¶ added in v0.4.0
func Reverse[T any](s []T) []T
Reverse reverses the elements of s in-place and returns s again for easy chaining.
func Shuffle ¶ added in v0.4.0
func Shuffle[T any](s []T) []T
Shuffle pseudo-randomizes the elements in the slice in-place. Returns the slice again for easy chaining.
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 Trim ¶ added in v0.9.0
func Trim[T comparable](s []T) []T
Trim removes any zero-value entries from the beginning and end of a given slice. The input slice is unchanged and a sub-slice is returned.
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.