slices

package
v0.12.6 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package slices offers various utilities for handling and manipulating slice data structures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk[T any](slice []T, n int) (cs [][]T)

Chunk returns a slice of slices of size n. If the slice cannot be evenly divided into chunks of size n, the last chunk will be smaller than size n.

func Compact

func Compact[T any](s []*T) (ret []*T)

Compact removes all nil values from the slice.

func Contains

func Contains[T comparable](s []T, c T) bool

Contains returns true if the slice contains the value.

func ContainsHashable

func ContainsHashable[H godash.Hashable](s []H, c H) bool

ContainsHashable returns true if the slice contains the value using the provided hash function.

func ContainsWith

func ContainsWith[S, C any](s []S, c C, pred func(S, C) bool) bool

ContainsWith returns true if the slice contains the value using the provided predicate.

func Difference

func Difference[T comparable](a, b []T) (ret []T)

Difference returns the difference between two slices.

func DifferenceHashable

func DifferenceHashable[H godash.Hashable](a, b []H) []H

DifferenceHashable returns the difference between two slices using the provided hash function.

func DifferenceWith

func DifferenceWith[T any](a, b []T, p func(T, T) bool) (ret []T)

DifferenceWith returns the difference between two slices using the provided predicate.

func Filter

func Filter[T any](s []T, p func(T) bool) (res []T)

Filter returns a new slice containing only the elements of the given slice that satisfy the given predicate.

func Flatten

func Flatten[T any](a [][]T) (b []T)

Flatten return a slice after flattening the input slice by one level.

func Foreach

func Foreach[T any](s []T, f func(T))

Foreach iterates over the given slice and calls the given function for each element. Should be used for side effects only since it doesn't return a value.

func Head[T any](s []T) T

Head returns the first element in a slice, this is the same as the 0th element.

func Intersection

func Intersection[T comparable](a []T, b []T) (c []T)

Intersection return the intersection of the two input slices. Standard union semantics apply. This is a fast implementation because it allows only comparable types.

func IntersectionHashable

func IntersectionHashable[T godash.Hashable](a []T, b []T) (c []T)

IntersectionHashable returns the intersection of the two input slices. Standard union semantics apply. This is a slow implementation, but it allows for custom equality semantics.

func IntersectionWith

func IntersectionWith[T any](a []T, b []T, p func(T, T) bool) (c []T)

IntersectionWith returns the intersection of the two input slices. Standard union semantics apply. This is a slow implementation, but it allows for custom equality semantics. The predicate p is used to determine whether two elements are equal.

func Map

func Map[T, U any](s []T, f func(T) U) (res []U)

Map applies the function f to each element of the slice and returns a new slice with the results.

func Reduce

func Reduce[T, U any](s []T, f func(U, T) U, init U) (res U)

Reduce applies the function f to each element of the slice and returns a single value.

func Sample added in v0.11.0

func Sample[T any](s []T) T

Sample returns a random element from the slice.

func SampleN added in v0.11.0

func SampleN[T any](s []T, n int) []T

SampleN returns n random elements from the slice.

func SampleNWithSeed added in v0.11.0

func SampleNWithSeed[T any](s []T, n int, seed int64) []T

SampleNWithSeed returns n random elements from the slice with the provided seed.

func SampleWithSeed added in v0.11.0

func SampleWithSeed[T any](s []T, seed int64) T

SampleWithSeed returns a random element from the slice with the provided seed.

func Shuffle added in v0.11.0

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

Shuffle shuffles the slice in place.

func ShuffleWithSeed added in v0.11.0

func ShuffleWithSeed[T any](s []T, seed int64) []T

ShuffleWithSeed shuffles the slice with the provided seed in place.

func Tail

func Tail[T any](s []T) []T

Tail returns all elements of the slice except the head. This is the same as the slice from index 1 to the end.

func Union

func Union[T comparable](a []T, b []T) (c []T)

Union returns a slice containing the elements of the two input slices. Standard union semantics apply. This is a fast implementation because it allows only comparable types.

func UnionHashable

func UnionHashable[T godash.Hashable](a []T, b []T) (c []T)

UnionHashable returns a slice containing the elements of the two input slices. Standard union semantics apply. This is a fast implementation because it allows only hashable types.

func UnionWith

func UnionWith[T any](a []T, b []T, p func(T, T) bool) (c []T)

UnionWith returns a slice containing the elements of the two input slices. Standard union semantics apply. This is a slow implementation, but it allows for custom equality semantics. The predicate p is used to determine whether two elements are equal.

func Unzip

func Unzip[T, U any](pairs []godash.Pair[T, U]) (a []T, b []U)

Unzip returns two slices, the first containing the first elements of the input pairs, and the second containing the second elements of the input pairs.

func Zip

func Zip[T, U any](a []T, b []U) (pairs []godash.Pair[T, U])

Zip returns a slice of pairs of elements from the two input slices. If the two slices are not of equal length, the function panics.

func ZipWithIndex

func ZipWithIndex[T any](s []T) (pairs []godash.Pair[int, T])

ZipWithIndex returns a slice of pairs of index and the matching element from the input slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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