Documentation
¶
Index ¶
- func Chunk[Type any](collection []Type, chunkSize int) [][]Type
- func Compact[T any](slice []*T) []*T
- func Contains[Type comparable](collection []Type, value Type) bool
- func Drop[T any](slice []T, n uint) []T
- func Every[Type any](collection []Type, predicate func(Type) bool) bool
- func Filter[Type any](collection []Type, predicate func(Type) bool) []Type
- func FoldLeft[In any, Out any](collection []In, f func(Out, In) Out, initial Out) Out
- func FoldRight[In any, Out any](collection []In, f func(In, Out) Out, initial Out) Out
- func GroupBy[Type any, Key any](slice []Type, keyFunc func(Type) Key) hashmap.HashMap[Key, []Type]
- func IsEmpty[Type any](collection []Type) bool
- func Map[From any, To any](collection []From, mapper func(it From) To) []To
- func RandShuffle[Type any](r *rand.Rand, input []Type) []Type
- func ReduceLeft[Type any](collection []Type, reducer func(Type, Type) Type) Type
- func ReduceRight[Type any](collection []Type, reducer func(Type, Type) Type) Type
- func Reverse[Type any](collection []Type) []Type
- func Shuffle[Type any](input []Type) []Type
- func Some[Type any](collection []Type, predicate func(Type) bool) bool
- func Sort[Type constraints.Ordered](collection []Type) []Type
- func SortBy[Type any](collection []Type, less func(Type, Type) bool) []Type
- func SortDescending[Type constraints.Ordered](collection []Type) []Type
- func Take[Type any](slice []Type, n uint) []Type
- func Zip[A, B any](a []A, b []B) []tuple.OfTwo[A, B]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶
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
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 ¶
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 ¶
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 ¶
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 ¶
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 Map ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.