reducer

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: Apache-2.0 Imports: 3 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[A, B any](s iterable.Iterable[A], reducer Reducer[A, B]) B

func ApplySlice

func ApplySlice[A, B any](s []A, reducer Reducer[A, B]) B

Types

type Number

type Number interface {
	byte | int | int32 | int64 | float32 | float64
}

Number type for built-in numbers

type Reducer

type Reducer[A, B any] func() ReducerInstance[A, B]

Reducer works on streams of A and produces a B

func Average

func Average[N Number]() Reducer[N, float64]

Average calculates the average value from the input

func Constant

func Constant[S, T any](value T) Reducer[S, T]

Constant is a reducer that always returns a constant value

func Count

func Count[A any]() Reducer[A, int]

Count the inputs.

func Distinct

func Distinct[A comparable, B any](r Reducer[A, B]) Reducer[A, B]

func DistinctBy

func DistinctBy[A, B any, C comparable](key func(A) C, r Reducer[A, B]) Reducer[A, B]

func Do

func Do[A any](f func(A)) Reducer[A, struct{}]

Do executes the function f for all elements in the input.

func DoErr

func DoErr[A any](f func(A) error) Reducer[A, error]

DoErr executes the function f for all elements in the input until an error occurs. If an error occurs, it is returned.

func Exists

func Exists[T any](cond func(T) bool) Reducer[T, bool]

Exists checks whether there is some element in the input that satisfies the given condition.

func Filter

func Filter[A, B any](cond func(A) bool, r Reducer[A, B]) Reducer[A, B]

func First

func First[A any]() Reducer[A, A]

First only returns the first element in the input (or zero if the input is empty)

func FlatMap

func FlatMap[A, B, C any](f func(A) iterable.Iterable[B], r Reducer[B, C]) Reducer[A, C]

func Forall

func Forall[T any](cond func(T) bool) Reducer[T, bool]

Forall checks whether all elements in the input satisfy 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 Limit

func Limit[A, B any](n int, next Reducer[A, B]) Reducer[A, B]

Limit the number of elements taken from the input to n

func Map

func Map[A, B, C any](f func(A) B, r Reducer[B, C]) Reducer[A, C]

func Max

func Max[N Number]() Reducer[N, N]

Max calculates the maximum number in the input

func Min

func Min[N Number]() Reducer[N, N]

Min calculates the minimum number in the input

func Product

func Product[N Number]() Reducer[N, N]

Product calculated the multiplication of all input values.

func Reduce

func Reduce[A, B any](start B, combine func(B, A) B) Reducer[A, B]

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

func Reduce0[A any](combine func(A, A) A) Reducer[A, A]

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 Skip

func Skip[A, B any](n int, next Reducer[A, B]) Reducer[A, B]

Skip the first n elements in the input

func Sorted

func Sorted[A, B any](cmp func(A, A) bool, next Reducer[A, B]) Reducer[A, B]

func Sum

func Sum[N Number]() Reducer[N, N]

Sum the input numbers.

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 ToSlice

func ToSlice[A any]() Reducer[A, []A]

func (Reducer[A, B]) Apply

func (r Reducer[A, B]) Apply(i iterable.Iterable[A]) B

func (Reducer[A, B]) ApplyIterator

func (r Reducer[A, B]) ApplyIterator(it iterable.Iterator[A]) B

func (Reducer[A, B]) ApplySlice

func (r Reducer[A, B]) ApplySlice(as []A) B

type ReducerInstance

type ReducerInstance[A, B any] struct {
	Complete func() B
	Step     func(A) bool
}

ReducerInstance is a temporary data structure created by the Reducer at the beginning of a pipeline.

Jump to

Keyboard shortcuts

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