slice

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package slice provides common utility functions to Go slices.

Index

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

func Collect[A any, B any](as []A, f func(a A) option.Option[B]) (bs []B)

Collect runs through the slice, executes the given function and only keeps good returned values.

func Count added in v0.2.3

func Count[A any](as []A, predicate Predicate[A]) (cnt int)

Count counts the number of elements that satisfy the given predicate.

func Filter added in v0.0.6

func Filter[A any](as []A, p Predicate[A]) (res []A)

Filter filters slice values.

func FilterNot added in v0.1.0

func FilterNot[A any](as []A, p Predicate[A]) (res []A)

FilterNot filters slice values inverting the condition.

func FlatMap

func FlatMap[A any, B any](as []A, f func(A) []B) (bs []B)

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 FoldLeft

func FoldLeft[A any, B any](as []A, zero B, f func(B, A) B) (res B)

FoldLeft folds all values in the slice using the combination function.

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

func Grouped[A any](as []A, size int) (res [][]A)

Grouped partitions the slice into groups of the given size. Last partition might be smaller.

func Len added in v0.1.3

func Len[A any](as []A) int

Len returns the length of the slice. This is a normal function that can be passed around unlike the built-in `len`.

func Map

func Map[A any, B any](as []A, f func(A) B) (bs []B)

Map converts all values of a slice using the provided function.

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.

func Sliding added in v0.1.2

func Sliding[A any](as []A, size int, step int) (res [][]A)

Sliding splits the provided slice into windows. Each window will have the given size. The first window starts from offset = 0. Each consecutive window starts at prev_offset + step. Last window might very well be shorter.

func Sum added in v0.1.0

func Sum[N Number](ns []N) (sum N)

Sum sums numbers.

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 Predicate added in v0.2.3

type Predicate[A any] func(A) bool

Predicate is a function with a boolean result type.

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.

Jump to

Keyboard shortcuts

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