collections

package
v0.5.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk[Type any](collection []Type, chunkSize int) [][]Type

Chunk splits a collection into chunks of a given size. The given chunk size must be greater than zero, otherwise it panics. Example:

_ = Chunk([]int{1, 2, 3, 4, 5}, 2) == [][]int{{1, 2}, {3, 4}, {5}}

func Compact added in v0.5.0

func Compact[T any](slice []*T) []*T

Compact returns a slice with all nil elements removed. The order of the elements is preserved.

func Contains

func Contains[Type comparable](collection []Type, value Type) bool

Contains determines if a collection contains a value.

func Drop added in v0.5.0

func Drop[T any](slice []T, n uint) []T

Drop returns a slice with the first n elements removed. If n is greater than the length of the slice, an empty slice is returned.

func Every

func Every[Type any](collection []Type, predicate func(Type) bool) bool

Every runs the given predicate function against each element in the collection. If the result of calling the predicate on all elements is `true`, then it returns `true`. If, at least one returns `false`, then it returns `false`.

func Filter

func Filter[Type any](collection []Type, predicate func(Type) bool) []Type

Filter calls the provided predicate function once for each element in the given collection slice, and constructs a new slice of all the elements for which the predicate returns true.

func FoldLeft

func FoldLeft[In any, Out any](collection []In, f func(Out, In) Out, initial Out) Out

FoldLeft folds the elements of the collection to a single value by combining them using the specified function associating left and the initial value. When applying an associative function, there is no difference between this and FoldRight. However, if the function is non-associative, the results are different due to the order of the associations. See also: FoldRight. Example:

subtract := func (a, b int) int { return a - b }
_ = FoldLeft[int]([]int{1, 2, 3, 4}, subtract, 9) == ((((9 - 1) - 2) - 3) - 4)

func FoldRight

func FoldRight[In any, Out any](collection []In, f func(In, Out) Out, initial Out) Out

FoldRight folds the elements of the collection to a single value by combining them using the specified function associating right and the initial value. When applying an associative function, there is no difference between this and FoldLeft. However, if the function is non-associative, the results are different due to the order of the associations. See also: FoldLeft. Example:

subtract := func (a, b int) int { return a - b }
_ = FoldRight[int]([]int{1, 2, 3, 4}, subtract, 9) == (1 - (2 - (3 - (4 - 9))))

func GroupBy added in v0.5.0

func GroupBy[Type any, Key any](slice []Type, keyFunc func(Type) Key) hashmap.HashMap[Key, []Type]

GroupBy groups a slice of items by a given key function.

func IsEmpty

func IsEmpty[Type any](collection []Type) bool

IsEmpty tells whether the given collection is empty.

func Map

func Map[From any, To any](collection []From, mapper func(it From) To) []To

Map calls the provided mapper function once for each element in the given collection slice, in order, and constructs a new slice from the results.

func RandShuffle

func RandShuffle[Type any](r *rand.Rand, input []Type) []Type

RandShuffle shuffles the given slice using the given randomizer. It returns a new slice containing the shuffled items.

func ReduceLeft

func ReduceLeft[Type any](collection []Type, reducer func(Type, Type) Type) Type

ReduceLeft reduces the elements of the collection to a single value by combining them using the specified function associating left. When applying an associative function, there is no difference between this and ReduceRight. However, if the function is non-associative, the results are different due to the order of the associations. Panics if the collection is empty. See also: ReduceRight. Example:

subtract := func (a, b int) int { return a - b }
_ = ReduceLeft[int]([]int{1, 2, 3, 4}, subtract) == (((1 - 2) - 3) - 4)

func ReduceRight

func ReduceRight[Type any](collection []Type, reducer func(Type, Type) Type) Type

ReduceRight reduces the elements of the collection to a single value by combining them using the specified function associating right. When applying an associative function, there is no difference between this and ReduceLeft. However, if the function is non-associative, the results are different due to the order of the associations. Panics if the collection is empty. See also: ReduceLeft. Example:

subtract := func (a, b int) int { return a - b }
_ = ReduceRight[int]([]int{1, 2, 3, 4}, subtract) == (1 - (2 - (3 - 4)))

func Reverse

func Reverse[Type any](collection []Type) []Type

Reverse reverses the order of the elements in the given collection.

func Shuffle

func Shuffle[Type any](input []Type) []Type

Shuffle shuffles the given slice using time.Now() as the random seed. It returns a new slice containing the shuffled items.

func Some

func Some[Type any](collection []Type, predicate func(Type) bool) bool

Some runs the given predicate function against each element in the collection. If the result of calling the predicate on at least one of the elements is `true`, then it returns `true`. If no elements satisfied the predicate, returns `false`.

func Sort

func Sort[Type constraints.Ordered](collection []Type) []Type

Sort sorts the given collection by the natural order of its elements in an ascending fashion.

func SortBy

func SortBy[Type any](collection []Type, less func(Type, Type) bool) []Type

SortBy sorts the given collection by the given sorting function. This function must return a boolean indicating whether the first argument should come before the second argument.

func SortDescending

func SortDescending[Type constraints.Ordered](collection []Type) []Type

SortDescending sorts the given collection by the natural order of its elements in a descending fashion.

func Take added in v0.5.0

func Take[Type any](slice []Type, n uint) []Type

Take returns the first n elements of a slice. If n is greater than the length of the slice, the entire slice is returned.

func Zip added in v0.5.0

func Zip[A, B any](a []A, b []B) []tuple.OfTwo[A, B]

Zip zips two collections into a collection of tuples of two. The resulting collection will be the size of the smaller collection. See also tuple.Of2.

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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