Documentation ¶
Overview ¶
Package slice provides common utility functions to Go slices.
Index ¶
- func AppendAll[A any](ass ...[]A) (aas []A)
- func Collect[A any, B any](as []A, f func(a A) option.Option[B]) (bs []B)
- func Count[A any](as []A, predicate Predicate[A]) (cnt int)
- func Filter[A any](as []A, p Predicate[A]) (res []A)
- func FilterNot[A any](as []A, p Predicate[A]) (res []A)
- func FlatMap[A any, B any](as []A, f func(A) []B) (bs []B)
- func Flatten[A any](ass [][]A) (aas []A)
- func FoldLeft[A any, B any](as []A, zero B, f func(B, A) B) (res B)
- func GroupBy[A any, K comparable](as []A, f func(A) K) (res map[K][]A)
- func GroupByMap[A any, K comparable, B any](as []A, f func(A) K, g func([]A) B) (res map[K]B)
- func GroupByMapCount[A any, K comparable](as []A, f func(A) K) (res map[K]int)
- func Grouped[A any](as []A, size int) (res [][]A)
- func Len[A any](as []A) int
- func Map[A any, B any](as []A, f func(A) B) (bs []B)
- func MapValues[K comparable, A any, B any](m map[K]A, f func(A) B) (res map[K]B)
- func SetSize[A comparable](s Set[A]) int
- func Sliding[A any](as []A, size int, step int) (res [][]A)
- func Sum[N Number](ns []N) (sum N)
- type Number
- type Predicate
- type Set
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendAll ¶ added in v0.1.2
func AppendAll[A any](ass ...[]A) (aas []A)
AppendAll concatenates all slices.
func Collect ¶ added in v0.2.6
Collect runs through the slice, executes the given function and only keeps good returned values.
func FlatMap ¶
FlatMap converts all values of a slice using the provided function. As the function returns slices, all of them are appended to a single long slice.
func Flatten ¶ added in v0.0.8
func Flatten[A any](ass [][]A) (aas []A)
Flatten simplifies a slice of slices to just a slice.
func GroupBy ¶ added in v0.1.2
func GroupBy[A any, K comparable](as []A, f func(A) K) (res map[K][]A)
GroupBy groups elements by a function that returns a key.
func GroupByMap ¶ added in v0.1.6
func GroupByMap[A any, K comparable, B any](as []A, f func(A) K, g func([]A) B) (res map[K]B)
GroupByMap is a convenience function that groups and then maps the subslices.
func GroupByMapCount ¶ added in v0.1.6
func GroupByMapCount[A any, K comparable](as []A, f func(A) K) (res map[K]int)
GroupByMapCount for each key counts how often it is seen.
func Grouped ¶ added in v0.1.2
Grouped partitions the slice into groups of the given size. Last partition might be smaller.
func Len ¶ added in v0.1.3
Len returns the length of the slice. This is a normal function that can be passed around unlike the built-in `len`.
func MapValues ¶ added in v0.1.3
func MapValues[K comparable, A any, B any](m map[K]A, f func(A) B) (res map[K]B)
MapValues converts values in the map using the provided function.
func SetSize ¶ added in v0.1.0
func SetSize[A comparable](s Set[A]) int
SetSize returns the size of the set.
Types ¶
type Number ¶ added in v0.1.0
type Number interface { int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64 | complex64 | complex128 }
Number is a generic number interface that covers all Go number types.
type Set ¶ added in v0.0.6
type Set[A comparable] map[A]struct{}
Set is a way to represent sets in Go.
func ToSet ¶ added in v0.0.6
func ToSet[A comparable](as []A) (s Set[A])
ToSet converts a slice to a set.