enum

package
v0.0.29 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: Apache-2.0 Imports: 3 Imported by: 53

Documentation

Overview

Package enum provides handy functional programming -style functions that operate on enumerable data types.

Index

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

func ChunkEvery[T any](items []T, n, step int) [][]T

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

func EachWithIndex[T any](items []T, f func(i int, value T))

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

func FlatMap[S any, T any](values []S, f func(S) []T) []T

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

func FlatMapWithIndex[S any, T any](values []S, f func(int, S) []T) []T

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 Identity

func Identity[T any](value T) T

Identity returns the value passed to it.

func Length

func Length[T any](items []T) int

Length returns the length of the provided slice.

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 Map

func Map[S any, T any](values []T, f func(value T) S) []S

Map maps a slice of values from one type to another using the provided func f.

func MapWithIndex

func MapWithIndex[S any, T any](values []T, f func(index int, value T) S) []S

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

func MaxFunc[T any](values []T, lessFunc func(a, b T) bool) (ret T)

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

func MinFunc[T any](values []T, lessFunc func(a, b T) bool) (ret T)

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 Reduce

func Reduce[S any, T any](items []S, acc T, f func(S, T) T) T

Reduce reduces a slice using an accumulator and `f(item, acc)`.

func ReduceWithIndex

func ReduceWithIndex[S any, T any](items []S, acc T, f func(int, S, T) T) T

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.

func Sum

func Sum[T Number](values []T) (ret T)

Sum sums up a slice of numbers.

func Uniq

func Uniq[T comparable](items []T) (ret []T)

Uniq removes all duplicated elements.

func Zip

func Zip[T any](lists [][]T) (ret [][]T)

Zip zips corresponding elements from slice of slices.

The zipping finishes as soon as any slice ends.

func Zip2

func Zip2[S any, T any, KV any](sList []S, tList []T, f func(S, T) KV) (ret []KV)

Zip2 zips corresponding elements from different types into a slice of structs.

The zipping finishes as soon as either slice ends.

Types

type FilterFunc

type FilterFunc[T any] func(T) bool

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

type FilterFuncWithIndex[T any] func(int, T) bool

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.

Jump to

Keyboard shortcuts

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