slices

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2023 License: MIT Imports: 0 Imported by: 0

README

slices

Generic operations on slices: map, reduce, split, and group by.
Because writing for loops over and over is really boring.

Documentation

Overview

Package slices provides generic operations on slices, including map, reduce, split, and group by.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 Split added in v0.2.0

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

Split given slice into equal sized parts. 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