slicesx

package
v0.0.0-...-d464d88 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: MIT, Unlicense Imports: 7 Imported by: 0

Documentation

Overview

Package slicesx provides slice utilities that compliment Go standard slices package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fanout

func Fanout[S ~[]E, E any](seq S, fn func(int, E))

Fanout across all elements of a slice in parallel. The function receives the index and element.

func FanoutErr

func FanoutErr[S ~[]E, E any](seq S, fn func(int, E) error) error

FanoutErr is like Fanout but returns an aggregated error.

func Filter

func Filter[S ~[]E, E any](s S, fn func(E) bool) S

Filter out elements from a slice when the predicate returns false.

func FilterMap

func FilterMap[S ~[]In, In any, Out any](s S, fn func(In) (Out, bool)) []Out

FilterMap maps [In] to [Out], ignoring entries that return false.

func Flatten

func Flatten[E any](s [][]E) []E

Flatten reduces a 2D slices to a 1D slice.

func For

func For[S ~[]E, E any](seq S, fn func(int, E))

For processes a sequence with the given function.

func ForErr

func ForErr[S ~[]E, E any](seq S, fn func(int, E) error) error

ForErr processes a sequence with the given function.

func Generate

func Generate[E any](n int, fn func(int) E) []E

Generate returns a slice of [n] items produced by [fn].

func Map

func Map[S ~[]E, E any, Out any](s S, fn func(E) Out) []Out

Map transforms a slice by applying a function to each element and returning the result.

func MapErr

func MapErr[S ~[]E, E any, Out any](s S, fn func(E) (Out, error)) (out []Out, err error)

MapErr transforms a slice by applying a fallible function to each element and returning the result.

If the function returns an error the map operation completes, returning an aggregated error for each failed transform. The output list will be short by the number of failures. Length of the output is not guaranteed to match that of the input.

func MapIndex

func MapIndex[S ~[]E, E any, Out any](s S, fn func(int, E) Out) []Out

MapIndex is like Map but provides the iteration index to the transform function.

func MapIndexSafe

func MapIndexSafe[S ~[]E, E any, Out any](s S, fn func(int, E) Out) (_ []Out, err error)

MapIndexSafe is like MapIndex but recovers from panics and returns an error instead.

func MapSafe

func MapSafe[S ~[]E, E any, Out any](s S, fn func(E) Out) (_ []Out, err error)

MapSafe is like Map but recovers from panics and returns an error instead.

func Normalize

func Normalize[S ~[]E, E comparable](s S) S

Normalize de-duplicates a slices of comparable elements. Doesn't rely on sorting.

func ParaMap

func ParaMap[S ~[]E, E any, Out any](s S, fn func(E) Out) []Out

ParaMap transforms a slice by applying a function to each element and returning the result. Transforms are executed concurrently.

func ParaMapErr

func ParaMapErr[S ~[]E, E any, Out any](s S, fn func(E) (Out, error)) (out []Out, err error)

ParaMapErr is MapErr, but all instances of fn run concurrently.

func PopBack

func PopBack[S ~[]E, E any](s *S) E

PopBack returns the last element from the slice.

func PopBackSafe

func PopBackSafe[S ~[]E, E any](s *S) (e E, ok bool)

PopBackSafe returns the last element from the slice, or false.

func PopFront

func PopFront[S ~[]E, E any](s *S) E

PopFront returns the first element from the slice.

func PopFrontSafe

func PopFrontSafe[S ~[]E, E any](s *S) (e E, ok bool)

PopFrontSafe returns the first element from the slice, or false.

func Repeat

func Repeat[E any](n int, v E) []E

Repeat returns a slice of [n] items of value [v].

func Take

func Take[S ~[]E, E any](s *S, index int) E

Take selects the item and removes it, without bounds checks.

func TakeAllFunc

func TakeAllFunc[S ~[]E, E any](s *S, fn func(E) bool) (S, bool)

TakeAllFunc selects all items for which [fn] returns true and removes them from the slice.

func TakeFunc

func TakeFunc[S ~[]E, E any](s *S, fn func(E) bool) (E, bool)

TakeFunc selects the first item that satisfies [fn] and removes it.

func TakeRange

func TakeRange[S ~[]E, E any](s *S, start, end int) S

TakeRange selects all items between [start] and [end] and removes them from the slice. Does not bounds check.

func TakeRangeSafe

func TakeRangeSafe[S ~[]E, E any](s *S, start, end int) (S, bool)

TakeRangeSafe selects all items between [start] and [end] and removes them from the slice. Checks the bounds.

func TakeSafe

func TakeSafe[S ~[]E, E any](s *S, index int) (E, bool)

TakeSafe selects the item and removes it, returning true if found.

func ToMap

func ToMap[S ~[]E, E any, K comparable](s S, keyFor func(int, E) K) map[K]E

ToMap converts a slice to a map, using [keyFor] to extract the key from each element.

Types

This section is empty.

Jump to

Keyboard shortcuts

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