Documentation ¶
Overview ¶
Package stream provides handy functional programming -style functions that operate on enumerable data types using channels.
Index ¶
- func All[T any](ch <-chan T, f FilterFunc[T]) bool
- func AllWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) bool
- func Any[T any](ch <-chan T, f FilterFunc[T]) bool
- func AnyWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) bool
- func ChunkEvery[T any](ch <-chan T, n, step int) <-chan []T
- func Count[T any](ch <-chan T, f FilterFunc[T]) int
- func CountWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) int
- func Dedup[T comparable](ch <-chan T) <-chan T
- func Each[T any](ch <-chan T, f func(item T))
- func EachWithIndex[T any](ch <-chan T, f func(i int, value T))
- func Filter[T any](ch <-chan T, f FilterFunc[T]) []T
- func FilterMap[S any, T any](ch <-chan S, m func(S) T, f FilterFunc[T]) []S
- func FilterWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) []T
- func Find[T any](ch <-chan T, f FilterFunc[T]) (ret T, ok bool)
- func FindOr[T any](ch <-chan T, f FilterFunc[T], defValue T) T
- func FindOrWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T], defValue T) T
- func FindWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) (ret T, ok bool)
- func First[T any](ch <-chan T) (ret T)
- func FlatMap[S any, T any](ch <-chan S, f func(S) []T) []T
- func FlatMapWithIndex[S any, T any](ch <-chan S, f func(int, S) []T) []T
- func Frequencies[T comparable](ch <-chan T) map[T]int
- func FrequenciesBy[S any, T comparable](ch <-chan S, keyFunc func(S) T) map[T]int
- func GroupBy[K comparable, V any, T any](ch <-chan T, keyFunc func(T) K, valueFunc func(T) V) map[K][]V
- func Iterate[T any](start T, f func(index int, last T) (val T, ok bool)) <-chan T
- func Length[T any](ch <-chan T) int
- func Map[S any, T any](ch <-chan T, f func(v T) S) []S
- func MapStream[S any, T any](ch <-chan T, f func(v T) S) <-chan S
- func Max[T constraints.Ordered](ch <-chan T) (ret T)
- func Member[T comparable](ch <-chan T, elem T) bool
- func Min[T constraints.Ordered](ch <-chan T) (ret T)
- func PermutationsOf[T any](sequence []T) <-chan []T
- func Product[T Number](ch <-chan T) (ret T)
- func Range[T int](start, end T) <-chan T
- func Ranges[T int](start, end []T) <-chan []T
- func Reduce[S any, T any](ch <-chan S, acc T, f func(S, T) T) T
- func ReduceWithIndex[S any, T any](ch <-chan S, acc T, f func(int, S, T) T) T
- func Repeatedly[T any](f func() T) <-chan T
- func Scan[T any](ch <-chan T, acc T, f func(a, b T) T) <-chan T
- func Sum[T Number](ch <-chan T) (ret T)
- func Take[T any](ch <-chan T, n int) []T
- func ToChan[T any](items []T) <-chan T
- func ToSlice[T any](ch <-chan T) []T
- func Uniq[T comparable](ch <-chan T) <-chan T
- func Zip2[S any, T any, KV any](sCh <-chan S, tCh <-chan T, f func(S, T) KV) <-chan KV
- type FilterFunc
- type FilterFuncWithIndex
- type Number
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All[T any](ch <-chan T, f FilterFunc[T]) bool
All returns true if all f(item) calls return true.
func AllWithIndex ¶
func AllWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) bool
AllWithIndex returns true if all f(index, item) calls return true.
func Any ¶
func Any[T any](ch <-chan T, f FilterFunc[T]) bool
Any returns true if any f(item) call returns true.
func AnyWithIndex ¶
func AnyWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) bool
AnyWithIndex returns true if any f(index, item) call returns true.
func ChunkEvery ¶
ChunkEvery takes a channel of values and chunks them n-at-a-time with the given step size. It discards any left-over items.
func Count ¶
func Count[T any](ch <-chan T, f FilterFunc[T]) int
Count returns the count of items in the channel for which `f(item)` returns true.
func CountWithIndex ¶
func CountWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) int
CountWithIndex returns the count of items in the channel for which `f(index, item)` returns true.
func Dedup ¶
func Dedup[T comparable](ch <-chan T) <-chan T
Dedup returns a channel where all consecutive duplicated elements are collapsed to a single element.
func Each ¶
func Each[T any](ch <-chan T, f func(item T))
Each processes each item with the provided function.
func EachWithIndex ¶
EachWithIndex iterates over a channel and calls the provided function with its index and value.
func Filter ¶
func Filter[T any](ch <-chan T, f FilterFunc[T]) []T
Filter filters a channel of values and keeps those for which f(value) returns true.
func FilterMap ¶
func FilterMap[S any, T any](ch <-chan S, m func(S) T, f FilterFunc[T]) []S
FilterMap filters a channel of mapped values and keeps those for which f(value) returns true.
func FilterWithIndex ¶
func FilterWithIndex[T any](ch <-chan T, f FilterFuncWithIndex[T]) []T
FilterWithIndex filters a channel of values and keeps those for which f(index, value) returns true.
func Find ¶
func Find[T any](ch <-chan 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](ch <-chan 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](ch <-chan 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](ch <-chan 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](ch <-chan T) (ret T)
First returns the first item of the provided chan 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](ch <-chan 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](ch <-chan 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](ch <-chan 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 Iterate ¶
Iterate returns a channel of values using the provided `f(index, last)` function. f returns false if this is the last value generated, and the channel will be closed.
func MapStream ¶
MapStream maps a channel of values from one type to another using the provided func f.
func Max ¶
func Max[T constraints.Ordered](ch <-chan T) (ret T)
Max returns the maximal element in the channel (or the zero value for an empty channel).
func Member ¶
func Member[T comparable](ch <-chan T, elem T) bool
Member checks if elem exists within the channel of values. The channel stops being read if the elem is found.
func Min ¶
func Min[T constraints.Ordered](ch <-chan T) (ret T)
Min returns the minimal element in the channel (or the zero value for an empty channel).
func PermutationsOf ¶ added in v0.0.34
func PermutationsOf[T any](sequence []T) <-chan []T
PermutationsOf streams all permutations of the given sequence to the returned channel.
func Product ¶
func Product[T Number](ch <-chan T) (ret T)
Product multiples a channel of numbers together or the zero value if the channel is empty.
func Range ¶
func Range[T int](start, end T) <-chan T
Range returns a channel 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) <-chan []T
Ranges returns a channel 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 Repeatedly ¶
func Repeatedly[T any](f func() T) <-chan T
Repeatedly returns a channel generated by calling `f` repeatedly. The channel does not close.
func Scan ¶
func Scan[T any](ch <-chan T, acc T, f func(a, b T) T) <-chan T
Scan creates a channel that 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.
func Sum ¶
func Sum[T Number](ch <-chan T) (ret T)
Sum sums up a channel of numbers or the zero value if the channel is empty.
func ToChan ¶
func ToChan[T any](items []T) <-chan T
ToChan returns a channel using the provided slice.
func Uniq ¶
func Uniq[T comparable](ch <-chan T) <-chan T
Uniq creates a stream that only emits elements if they are unique.
Keep in mind that, in order to know if an element is unique or not, this function needs to store all unique values emitted by the stream. Therefore, if the stream is infinite, the number of elements stored will grow infinitely, never being garbage-collected.
Types ¶
type FilterFunc ¶
FilterFunc takes a value and returns true if the value is to be kept.
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.Unsigned | constraints.Float | constraints.Complex }
Number has the "+" operator.