slice

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Package slice contains generic utility functions on slices

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](l []T, v T) bool

Contains checks for the existence of an element in the slice

func Deduplicate

func Deduplicate[T comparable](in []T) []T

Deduplicate removes duplicates in a slice

func Difference added in v1.0.0

func Difference[T comparable](as []T, bs []T) []T

Difference returns the difference of two sets (A - B)

func Filter

func Filter[T any](in []T, f func(T) bool) (out []T)

Filter filters a slice using the given function

func Fold

func Fold[A, B any](as []A, f func(A, B) B, acc B) B

Fold folds the slice into a single value using the given function and accumulator

func Index

func Index[T comparable](ts []T, v T) int

Index returns the index of a value if it exists it will return -1 if it does not exist in the slice

func Intersection added in v1.0.0

func Intersection[T comparable](as []T, bs []T) []T

Intersection returns the intersection of two slices, a slice of everything contained in both slices.

func Map

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

Map create a new slice from an existing one using a map function

func MapFilter

func MapFilter[A, B any](slice []A, f func(A) (B, bool)) (out []B)

MapFilter combines Map and Filter into one process It takes a function that takes a value of type A and returns a value of type B, and a bool for whether or not to keep the value.

func Max added in v1.0.0

func Max[A constraints.Ordered](as []A) A

Max returns the maximum element in a slice

func MaxFunc added in v1.0.0

func MaxFunc[A any](as []A, less func(A, A) bool) A

MaxFunc returns the maximum element in a slice using a given "less than" function

func Min added in v1.0.0

func Min[A constraints.Ordered](as []A) A

Min returns the minimum element in a slice

func MinFunc added in v1.0.0

func MinFunc[A any](as []A, less func(A, A) bool) A

MinFunc returns the minimum element in a slice using a given "less than function"

func Reduce

func Reduce[A, B any](as []A, f func(A, B) B, acc B) B

Reduce reduces the slice to a single value using the given fucntion and accumulator

func Remove

func Remove[T comparable](xs []T, d T) []T

Remove will remove from the slice the first match of the given value

func RemoveAll

func RemoveAll[T comparable](xs []T, v T) []T

RemoveAll removes all occurances of the given value in the slice

func Reverse

func Reverse[T any](a []T) []T

Reverse reverses the order of a slice

func Shuffle

func Shuffle[T any](l []T) []T

Shuffle shuffles a slice using the Fisher-Yates algorithm you need to call `rand.Seed` for this to work properly

func Sort

func Sort[T any](l []T, less func(T, T) bool) []T

Sort sorts a slice

func ToMap

func ToMap[K comparable, V, A any](as []A, f func(A) (K, V)) map[K]V

ToMap converts a slice to a map using the given function which takes an element and returns a key and value

func Union added in v1.0.0

func Union[T comparable](as []T, bs []T) []T

Union returns the union of two slices

Duplicates in the first slice will be left. Duplicates in the second slice will be removed.

Types

This section is empty.

Jump to

Keyboard shortcuts

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