slice

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2023 License: BSD-3-Clause Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy[T any](slice []T) []T

Copy returns a shallow copy of the given slice.

func Delete added in v0.9.0

func Delete[T any](s []T, shouldDelete func(T) bool) []T

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

func Gen[T any](sz int, generator func() T) []T

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

func GenIndex[T any](sz int, generator func(i int) T) []T

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

func MappingIndex[S, T any](slice []S, f func(i int, s S) T) []T

MappingIndex converts a slice from one type to another. The mapping function also receives the index of the element being transformed.

func Reduce

func Reduce[E any, R any](collector func(R, E) R, result R, from []E) R

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.

func Zero

func Zero[T any](slice []T) []T

Zero zeroes all elements in a slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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