Documentation ¶
Overview ¶
Package reducer implements reducers, similar to the Reducer concept in Clojure (https://clojure.org/reference/reducers).
Unfortunately, GO's generic implementation is too limited to make this abstraction pleasant to work with. In most cases, it is recommended to use the iterable package instead.
However, reducers simplify the implementation of some algorithms, for example the lazy quicksort implemented in this package.
Index ¶
- func Apply[A, B any](s iterable.Iterable[A], reducer Reducer[A, B]) B
- func ApplySlice[A, B any](s []A, reducer Reducer[A, B]) B
- type Number
- type Reducer
- func Average[N Number]() Reducer[N, float64]
- func Constant[S, T any](value T) Reducer[S, T]
- func Count[A any]() Reducer[A, int]
- func Distinct[A comparable, B any](r Reducer[A, B]) Reducer[A, B]
- func DistinctBy[A, B any, C comparable](key func(A) C, r Reducer[A, B]) Reducer[A, B]
- func Do[A any](f func(A)) Reducer[A, struct{}]
- func DoErr[A any](f func(A) error) Reducer[A, error]
- func Exists[T any](cond func(T) bool) Reducer[T, bool]
- func Filter[A, B any](cond func(A) bool, r Reducer[A, B]) Reducer[A, B]
- func First[A any]() Reducer[A, A]
- func FlatMap[A, B, C any](f func(A) iterable.Iterable[B], r Reducer[B, C]) Reducer[A, C]
- func Forall[T any](cond func(T) bool) Reducer[T, bool]
- func GroupBy[A any, K comparable, V any](key func(A) K, valReduce Reducer[A, V]) Reducer[A, map[K]V]
- func GroupByCollect[V any, K comparable](key func(V) K) Reducer[V, map[K][]V]
- func Limit[A, B any](n int, next Reducer[A, B]) Reducer[A, B]
- func Map[A, B, C any](f func(A) B, r Reducer[B, C]) Reducer[A, C]
- func Max[N Number]() Reducer[N, N]
- func Min[N Number]() Reducer[N, N]
- func Product[N Number]() Reducer[N, N]
- func Reduce[A, B any](start B, combine func(B, A) B) Reducer[A, B]
- func Reduce0[A any](combine func(A, A) A) Reducer[A, A]
- func Skip[A, B any](n int, next Reducer[A, B]) Reducer[A, B]
- func Sorted[A, B any](cmp func(A, A) bool, next Reducer[A, B]) Reducer[A, B]
- func Sum[N Number]() Reducer[N, N]
- func ToMap[T any, K comparable, V any](key func(T) K, value func(T) V) Reducer[T, map[K]V]
- func ToMapId[V any, K comparable](key func(V) K) Reducer[V, map[K]V]
- func ToSet[T comparable]() Reducer[T, map[T]bool]
- func ToSlice[A any]() Reducer[A, []A]
- type ReducerInstance
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplySlice ¶
Types ¶
type Reducer ¶
type Reducer[A, B any] func() ReducerInstance[A, B]
Reducer works on streams of A and produces a B
func DistinctBy ¶
func DistinctBy[A, B any, C comparable](key func(A) C, r Reducer[A, B]) Reducer[A, B]
func DoErr ¶
DoErr executes the function f for all elements in the input until an error occurs. If an error occurs, it is returned.
func Exists ¶
Exists checks whether there is some element in the input that satisfies the given condition.
func GroupBy ¶
func GroupBy[A any, K comparable, V any](key func(A) K, valReduce Reducer[A, V]) Reducer[A, map[K]V]
GroupBy groups the input using the given key function. For each key, one instance of the valReducer is created to further process the values with that key.
func GroupByCollect ¶
func GroupByCollect[V any, K comparable](key func(V) K) Reducer[V, map[K][]V]
GroupByCollect groups the input using the given key function. The resulting map contains all values with the same key as a slice under the same key.
func Reduce ¶
Reduce is a generic reduction function that processes the input from left to right. Initially, the state is equal to start. Then, combine is called for each input together with the current state. Finally, the state is returned.
func Reduce0 ¶
Reduce0 is like Reduce, but uses the first element in the input as the starting value. Returns the zero value when the input is empty.
func ToMap ¶
func ToMap[T any, K comparable, V any](key func(T) K, value func(T) V) Reducer[T, map[K]V]
ToMap turns the input into a map using the given key and value functions to extract key and value from elements in the input. If keys appear multiple times, only take the first key.
func ToMapId ¶
func ToMapId[V any, K comparable](key func(V) K) Reducer[V, map[K]V]
ToMapId turns the input into a map using the given key function to extract a key from elements in the input.
func ToSet ¶
func ToSet[T comparable]() Reducer[T, map[T]bool]
ToSet turns the input into a set represented as a map[T]bool
func (Reducer[A, B]) ApplyIterator ¶
func (Reducer[A, B]) ApplySlice ¶
func (r Reducer[A, B]) ApplySlice(as []A) B
type ReducerInstance ¶
ReducerInstance is a temporary data structure created by the Reducer at the beginning of a pipeline.