Documentation
¶
Overview ¶
Package enum provides handy functional programming -style functions that operate on enumerable data types.
Index ¶
- func All[T any](items []T, f FilterFunc[T]) bool
- func AllWithIndex[T any](items []T, f FilterFuncWithIndex[T]) bool
- func Any[T any](items []T, f FilterFunc[T]) bool
- func AnyWithIndex[T any](items []T, f FilterFuncWithIndex[T]) bool
- func Average[T Number](values []T) (ret T)
- func ChunkEvery[T any](items []T, n, step int) [][]T
- func Count[T any](items []T, f FilterFunc[T]) int
- func CountWithIndex[T any](items []T, f FilterFuncWithIndex[T]) int
- func Dedup[T comparable](values []T) []T
- func Each[T any](items []T, f func(item T))
- func EachWithIndex[T any](items []T, f func(i int, value T))
- func Filter[T any](values []T, f FilterFunc[T]) []T
- func FilterMap[S any, T any](values []S, m func(S) T, f FilterFunc[T]) []S
- func FilterWithIndex[T any](values []T, f FilterFuncWithIndex[T]) []T
- func Find[T any](values []T, f FilterFunc[T]) (ret T, ok bool)
- func FindOr[T any](values []T, f FilterFunc[T], defValue T) T
- func FindOrWithIndex[T any](values []T, f FilterFuncWithIndex[T], defValue T) T
- func FindWithIndex[T any](values []T, f FilterFuncWithIndex[T]) (ret T, ok bool)
- func First[T any](items []T) (ret T)
- func FlatMap[S any, T any](values []S, f func(S) []T) []T
- func FlatMapWithIndex[S any, T any](values []S, f func(int, S) []T) []T
- func Frequencies[T comparable](items []T) map[T]int
- func FrequenciesBy[S any, T comparable](items []S, keyFunc func(S) T) map[T]int
- func GroupBy[K comparable, V any, T any](items []T, keyFunc func(T) K, valueFunc func(T) V) map[K][]V
- func Identity[T any](value T) T
- func Length[T any](items []T) int
- func Longer[T any](a, b []T) []T
- func Map[S any, T any](values []T, f func(value T) S) []S
- func MapWithIndex[S any, T any](values []T, f func(index int, value T) S) []S
- func Max[T constraints.Ordered](values []T) (ret T)
- func MaxFunc[T any](values []T, lessFunc func(a, b T) bool) (ret T)
- func Member[T comparable](values []T, elem T) bool
- func Min[T constraints.Ordered](values []T) (ret T)
- func MinFunc[T any](values []T, lessFunc func(a, b T) bool) (ret T)
- func Product[T Number](values []T) (ret T)
- func Range[T Number](start, end T) (ret []T)
- func Ranges[T int](start, end []T) (ret [][]T)
- func Reduce[S any, T any](items []S, acc T, f func(S, T) T) T
- func ReduceWithIndex[S any, T any](items []S, acc T, f func(int, S, T) T) T
- func Scan[T any](items []T, acc T, f func(a, b T) T) (ret []T)
- func Shorter[T any](a, b []T) []T
- func Sum[T Number](values []T) (ret T)
- func Uniq[T comparable](items []T) (ret []T)
- func Zip[T any](lists [][]T) (ret [][]T)
- func Zip2[S any, T any, KV any](sList []S, tList []T, f func(S, T) KV) (ret []KV)
- type FilterFunc
- type FilterFuncWithIndex
- type Number
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All[T any](items []T, f FilterFunc[T]) bool
All returns true if all f(item) calls return true.
func AllWithIndex ¶
func AllWithIndex[T any](items []T, f FilterFuncWithIndex[T]) bool
AllWithIndex returns true if all f(index, item) calls return true.
func Any ¶
func Any[T any](items []T, f FilterFunc[T]) bool
Any returns true if any f(item) call returns true.
func AnyWithIndex ¶
func AnyWithIndex[T any](items []T, f FilterFuncWithIndex[T]) bool
AnyWithIndex returns true if any f(index, item) call returns true.
func Average ¶ added in v0.0.27
func Average[T Number](values []T) (ret T)
Average returns the average of a slice of numbers.
func ChunkEvery ¶
ChunkEvery takes a slice of items and chunks them n-at-a-time with the given step size. It discards any left-over items.
func Count ¶
func Count[T any](items []T, f FilterFunc[T]) int
Count returns the count of items in the slice for which `f(item)` returns true.
func CountWithIndex ¶
func CountWithIndex[T any](items []T, f FilterFuncWithIndex[T]) int
CountWithIndex returns the count of items in the slice for which `f(index, item)` returns true.
func Dedup ¶
func Dedup[T comparable](values []T) []T
Dedup returns a slice where all consecutive duplicated elements are collapsed to a single element.
func Each ¶
func Each[T any](items []T, f func(item T))
Each processes each item with the provided function.
func EachWithIndex ¶
EachWithIndex iterates over a slice and calls the provided function with its index and value.
func Filter ¶
func Filter[T any](values []T, f FilterFunc[T]) []T
Filter filters a slice of values and keeps those for which f(value) returns true.
func FilterMap ¶
func FilterMap[S any, T any](values []S, m func(S) T, f FilterFunc[T]) []S
FilterMap filters a slice of mapped values and keeps those for which f(value) returns true.
func FilterWithIndex ¶
func FilterWithIndex[T any](values []T, f FilterFuncWithIndex[T]) []T
FilterWithIndex filters a slice of values and keeps those for which f(index, value) returns true.
func Find ¶
func Find[T any](values []T, f FilterFunc[T]) (ret T, ok bool)
Find returns the first element for which f(value) returns true along with a boolean indicating a value was found.
func FindOr ¶
func FindOr[T any](values []T, f FilterFunc[T], defValue T) T
FindOr returns the first element for which f(value) returns true. If no element is found, defValue is returned.
func FindOrWithIndex ¶
func FindOrWithIndex[T any](values []T, f FilterFuncWithIndex[T], defValue T) T
FindOrWithIndex returns the first element for which f(index, value) returns true. If no element is found, defValue is returned.
func FindWithIndex ¶
func FindWithIndex[T any](values []T, f FilterFuncWithIndex[T]) (ret T, ok bool)
FindWithIndex returns the first element for which f(index, value) returns true along with a boolean indicating a value was found.
func First ¶
func First[T any](items []T) (ret T)
First returns the first item of the provided slice or its zero value.
func FlatMap ¶
FlatMap maps the given f(value) and flattens the result.
For example:
FlatMap([]int{1,2,3}, func (v int) []string {
s := strconv.Itoa(v)
return []string{s,s}
})
returns:
[]string{"1","1","2","2","3","3"}
func FlatMapWithIndex ¶
FlatMapWithIndex maps the given f(index, value) and flattens the result.
func Frequencies ¶
func Frequencies[T comparable](items []T) map[T]int
Frequencies returns a map with keys as unique elements of the provided items and the values as the count of every item.
func FrequenciesBy ¶
func FrequenciesBy[S any, T comparable](items []S, keyFunc func(S) T) map[T]int
FrequenciesBy returns a map with keys as unique elements of keyFunc(item) and the values as the count of every item.
func GroupBy ¶
func GroupBy[K comparable, V any, T any](items []T, keyFunc func(T) K, valueFunc func(T) V) map[K][]V
GroupBy splits the items into groups based on keyFunc and valueFunc.
For example:
GroupBy([]string{"ant", "buffalo", "cat", "dingo"}, StrLength, Identity[string])
returns:
{3: {"ant", "cat"}, 5: {"dingo"}, 7: {"buffalo"}}
and
GroupBy([]string{ant buffalo cat dingo}, StrLength, StrFirst)
returns:
{3: {"a", "c"}, 5: {"d"}, 7: {"b"}}
func Longer ¶
func Longer[T any](a, b []T) []T
Longer returns the longer slice. If len(a)==len(b), then a is preferred.
func MapWithIndex ¶
MapWithIndex maps a slice of values from one type to another using the provided func f.
func Max ¶
func Max[T constraints.Ordered](values []T) (ret T)
Max returns the maximal element in the slice (or the zero value for an empty slice).
func MaxFunc ¶
MaxFunc returns the maximal element in the slice (or the zero value for an empty slice) using the provided lessFunc.
func Member ¶
func Member[T comparable](values []T, elem T) bool
Member checks if elem exists within values.
func Min ¶
func Min[T constraints.Ordered](values []T) (ret T)
Min returns the minimal element in the slice (or the zero value for an empty slice).
func MinFunc ¶
MinFunc returns the minimal element in the slice (or the zero value for an empty slice) using the provided lessFunc.
func Product ¶
func Product[T Number](values []T) (ret T)
Product multiples a slice of numbers together.
func Range ¶
func Range[T Number](start, end T) (ret []T)
Range returns a slice of numbers that run from start to end.
For example:
Ranges(0, 2), Ranges(2, 0)
respectively return:
[]int{0, 1, 2}, []int{2, 1, 0}
func Ranges ¶
func Ranges[T int](start, end []T) (ret [][]T)
Ranges returns a slice of slices-of-integers that increment all values from the start to the end.
For example:
Ranges([]int{0,3,0}, []int{2,1,0})
returns:
[][]int{{0,3,0}, {1,2,0}, {2,1,0}}
Note that one of the ranges might overshoot if the distances are not identical.
func ReduceWithIndex ¶
ReduceWithIndex reduces a slice using an accumulator and `f(index, item, acc)`.
func Scan ¶
func Scan[T any](items []T, acc T, f func(a, b T) T) (ret []T)
Scan applies the given function to each element, emits the result and uses the same result as the accumulator for the next computation. It uses the given acc as the starting value.
For example:
Scan(Range(1,5), 0, func(a, b int) int { return a + b })
returns:
[]int{1, 3, 6, 10, 15}
func Shorter ¶
func Shorter[T any](a, b []T) []T
Shorter returns the shorter slice. If len(a)==len(b), then b is preferred.
Types ¶
type FilterFunc ¶
FilterFunc takes a value and returns true if the value is to be kept.
func Equals ¶
func Equals[T comparable](value T) FilterFunc[T]
Equals returns a function that checks if a value is equal to a given value.
type FilterFuncWithIndex ¶
FilterFuncWithIndex takes an index and value and returns true if the value is to be kept.
type Number ¶
type Number interface {
constraints.Integer | constraints.Float
}
Number has the "+" operator.