slice

package module
v0.0.0-...-1997cc2 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: MIT Imports: 4 Imported by: 0

README

Slice

Go Reference

Elixir's Enum module implemented in Go using generics.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[Element any](elements []Element, fun func(Element) bool) bool

All returns true if fun returns true for all elements in the slice.

func Any

func Any[Element any](elements []Element, fun func(Element) bool) bool

Any returns true if fun returns true for at least one element in the slice.

func Count

func Count[Element any](elements []Element) int

Count counts the number of elements in the slice.

func CountBy

func CountBy[Element any](elements []Element, fun func(Element) bool) int

CountBy counts the number of elements in slice where fun returns true.

func Each

func Each[Element any](elements []Element, fun func(Element))

Each invokes fun on each element in the slice.

func Filter

func Filter[Element any](elements []Element, fun func(Element) bool) []Element

Filter returns elements where fun returns true.

func FlatMap

func FlatMap[Element any](elements []Element, fun func(Element) []Element) []Element

FlatMap maps the given fun over slice and flattens the result.

func Frequencies

func Frequencies[Element comparable](elements []Element) map[Element]int

Frequencies returns a map with keys as unique elements and values as the count of every element.

func FrequenciesBy

func FrequenciesBy[Element any, Key comparable](elements []Element, fun func(Element) Key) map[Key]int

FrequenciesBy returns a map with keys as unique elements given by key_fun and values as the count of every element.

func GroupBy

func GroupBy[Element any, GroupBy comparable](elements []Element, fun func(Element) GroupBy) map[GroupBy][]Element

GroupBy splits the slice into groups based on key_fun.

func IsMember

func IsMember[Element comparable](elements []Element, member Element) bool

IsMember checks if element exists in the slice.

func IsMemberBy

func IsMemberBy[Element any, IsMemberBy comparable](elements []Element, member Element, fun func(Element) IsMemberBy) bool

IsMemberBy checks if element exists in the slice according to fun.

func Map

func Map[Element any, ReturnElement any](elements []Element, fun func(Element) ReturnElement) []ReturnElement

Map invokes fun on each element in the slice.

func Max

func Max[Element constraints.Ordered](elements []Element) Element

Max returns the maximum element in the slice.

func MaxBy

func MaxBy[Element any, CompareBy constraints.Ordered](elements []Element, fun func(Element) CompareBy) Element

MaxBy returns the maximum element in the slice according to fun.

func Min

func Min[Element constraints.Ordered](elements []Element) Element

Min returns the minimum element in the slice.

func MinBy

func MinBy[Element any, CompareBy constraints.Ordered](elements []Element, fun func(Element) CompareBy) Element

MinBy returns the minimum element in the slice according to fun.

func MinMax

func MinMax[Element constraints.Ordered](elements []Element) (Element, Element)

MinMax returns the minimum and maximum element in the slice.

func MinMaxBy

func MinMaxBy[Element any, CompareBy constraints.Ordered](elements []Element, fun func(Element) CompareBy) (Element, Element)

MinMaxBy returns the minimum and maximum element in the slice according to fun.

func Product

func Product[Element Number](elements []Element) Element

Product returns the product of all elements.

func ProductBy

func ProductBy[Element any, Product Number](elements []Element, fun func(Element) Product) Product

ProductBy returns the product of all elements according to fun.

func Random

func Random[Element any](elements []Element, seed ...int64) Element

Random returns a random element from the slice.

func Reduce

func Reduce[Element any, Accumulator any](elements []Element, fun func(Element, Accumulator) Accumulator, accumulator Accumulator) Accumulator

Reduce invokes fun on each element in the slice with the accumulator.

func ReduceWhile

func ReduceWhile[Element any, Accumulator any](elements []Element, fun func(Element, Accumulator) (Reduction, Accumulator), accumulator Accumulator) Accumulator

ReduceWhile invokes fun on each element in the slice with the accumulator until Halt is returned.

func Reject

func Reject[Element any](elements []Element, fun func(Element) bool) []Element

Reject returns elements excluding those where fun returns true.

func Reverse

func Reverse[Element comparable](elements []Element) []Element

Reverse returns a slice of elements in reverse order.

func Shuffle

func Shuffle[Element any](elements []Element, seed ...int64) []Element

Shuffle returns a list with the elements of the slice shuffled.

func Sort

func Sort[Element constraints.Ordered](elements []Element, order Order) []Element

Sort returns a slice sorted according to fun.

func SortBy

func SortBy[Element any, SortBy constraints.Ordered](elements []Element, fun func(Element) SortBy, order Order) []Element

SortBy returns a slice sorted according to fun.

func SplitWhile

func SplitWhile[Element any](elements []Element, fun func(Element) bool) ([]Element, []Element)

SplitWhile splits the slice in two at the position of the element for which fun returns a false for the first time.

func SplitWith

func SplitWith[Element any](elements []Element, fun func(Element) bool) ([]Element, []Element)

SplitWith splits the slice in two lists according to the given function fun.

func Sum

func Sum[Element Number](elements []Element) Element

Sum returns the sum of all elements.

func SumBy

func SumBy[Element any, SumBy Number](elements []Element, fun func(Element) SumBy) SumBy

SumBy returns the sum of all elements according to fun.

func Take

func Take[Element any](elements []Element, amount uint) []Element

Take takes an amount of elements from the beginning of the slice.

func TakeWhile

func TakeWhile[Element any](elements []Element, fun func(Element) bool) []Element

TakeWhile takes the elements from the beginning of the slice while fun returns a truthy value.

func Uniq

func Uniq[Element comparable](elements []Element) []Element

Uniq iterates over the slice, removing all duplicated elements.

func UniqBy

func UniqBy[Element any, UniqBy comparable](elements []Element, fun func(Element) UniqBy) []Element

UniqBy iterates over the slice, removing all duplicated elements according to fun.

Types

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

type Order

type Order int
const (
	Asc Order = iota
	Desc
)

type Reduction

type Reduction int
const (
	Cont Reduction = iota
	Halt
)

Jump to

Keyboard shortcuts

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