slices

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2024 License: MIT Imports: 0 Imported by: 0

README

slices

Generic operations on slices because we've got better things to do than write for-loops over and over.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk added in v0.9.0

func Chunk[X any](src []X, numParts int) [][]X

Chunk given slice into a fixed number of parts.

  • Chunk: fixed number of parts, variable part size.
  • Split: variable number of parts, fixed part size.

NOTE: If the number of parts exceeds the size of the source slice then the resulting slice will contain one element slices for each element in the source slice and nil slices for the remaining entries.

func Contains added in v0.4.3

func Contains[V comparable](src []V, wanted V) bool

Contains returns true when the slice contains the wanted value.

func ContainsAll added in v0.4.3

func ContainsAll[V any](src []V, selector func(V) bool) bool

ContainsAll returns true when the selector returns true for all values in the slice.

func ContainsAllErr added in v0.4.3

func ContainsAllErr[V any](src []V, selector func(V) (bool, error)) (bool, error)

ContainsAllErr allows the selector to return an error which is then returned to the caller.

func ContainsAny added in v0.4.3

func ContainsAny[V any](src []V, selector func(V) bool) bool

ContainsAny returns true when the slice contains a value for which the selector returns true.

func ContainsAnyErr added in v0.4.3

func ContainsAnyErr[V any](src []V, selector func(V) (bool, error)) (bool, error)

ContainsAnyErr allows the selector to return an error which is then returned to the caller.

func First added in v0.4.3

func First[V any](src []V, selector func(V) bool) V

First returns the first value in the slice where the selector returned true, or the zero value of V if the selector never returns true.

func FirstErr added in v0.4.3

func FirstErr[V any](src []V, selector func(V) (bool, error)) (V, error)

FirstErr allows the selector to return an error which is then returned to the caller.

func GroupBy added in v0.2.4

func GroupBy[K comparable, V any](src []V, keyFn func(V) K) map[K][]V

GroupBy indexes the given values by the result of the key function.

func GroupByErr added in v0.2.7

func GroupByErr[K comparable, V any](src []V, keyFn func(V) (K, error)) (map[K][]V, error)

GroupByErr allows the key function to fail and returns the failing error.

func Index added in v0.4.3

func Index[V comparable](src []V, wanted V) int

Index returns the first index in the slice that contains the wanted value, or -1 if not found.

func IndexOf added in v0.4.3

func IndexOf[V any](src []V, selector func(V) bool) int

IndexOf returns the first index in the slice where the selector returned true, or -1 if not found.

func IndexOfErr added in v0.4.3

func IndexOfErr[V any](src []V, selector func(V) (bool, error)) (int, error)

IndexOfErr allows the selector to return an error which is then returned to the caller.

func Map added in v0.2.0

func Map[A any, B any](src []A, mapper func(A) B) []B

Map converts from one type to another using a mapper function.

func MapErr added in v0.2.0

func MapErr[A any, B any](src []A, mapper func(A) (B, error)) ([]B, error)

MapErr allows the mapper function to fail and returns the failing error.

func Reduce added in v0.2.0

func Reduce[V any, A any](values []V, acc A, reducer func(acc A, val V) A) A

Reduce a slice to a single value given an initial value and a reducer function.

func ReduceErr added in v0.2.0

func ReduceErr[V any, A any](values []V, acc A, reducer func(acc A, val V) (A, error)) (A, error)

ReduceErr allows the reducer function to fail and returns the failing error.

func Select added in v0.4.1

func Select[V any](src []V, selector func(V) bool) []V

Select returns a slice with all values where the selector returned true.

func SelectErr added in v0.4.1

func SelectErr[V any](src []V, selector func(V) (bool, error)) ([]V, error)

SelectErr allows the selector to return an error which is then returned to the caller.

func Split added in v0.2.0

func Split[X any](src []X, partLen int) [][]X

Split given slice into equal sized parts.

  • Chunk: fixed number of parts, variable part size.
  • Split: variable number of parts, fixed part size.

NOTE: The last part may have an unequal length if the size of the source slice is not divisible by the required length without leaving a remainder.

Types

This section is empty.

Jump to

Keyboard shortcuts

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