sliceutil

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2024 License: MPL-2.0 Imports: 0 Imported by: 3

Documentation

Overview

Package sliceutil contains helpers related to slices, usually ones that are generic-related, and are broadly useful, but which the Go core team, in its infinite wisdom, has decided are too much power for the unwashed mashes, and therefore omitted from the utilities in `slices`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultIfEmpty added in v0.12.0

func DefaultIfEmpty[T any](input []T, defaultSlice []T) []T

DefaultIfEmpty returns the default slice if the input slice is nil or empty, otherwise it returns the input slice.

func GroupBy

func GroupBy[T any, U comparable](collection []T, keyFunc func(T) U) map[U][]T

GroupBy returns an object composed of keys generated from the results of running each element of collection through keyFunc.

func KeyBy

func KeyBy[T any, K comparable, V any](collection []T, tupleFunc func(item T) (K, V)) map[K]V

KeyBy converts a slice into a map using the key/value tuples returned by tupleFunc. If any two pairs would have the same key, the last one wins. Go maps are unordered and the order of the new map isn't guaranteed to the same as the original slice.

func Map

func Map[T any, R any](collection []T, mapFunc func(T) R) []R

Map manipulates a slice and transforms it to a slice of another type.

Types

This section is empty.

Jump to

Keyboard shortcuts

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