stream

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 4 Imported by: 2

Documentation

Overview

Package stream provides handy functional programming -style functions that operate on enumerable data types using channels.

Index

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

func ChunkEvery[T any](ch <-chan T, n, step int) <-chan []T

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

func EachWithIndex[T any](ch <-chan T, f func(i int, value T))

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

func FlatMap[S any, T any](ch <-chan 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](ch <-chan S, f func(int, S) []T) []T

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

func Iterate[T any](start T, f func(index int, last T) (val T, ok bool)) <-chan T

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 Length

func Length[T any](ch <-chan T) int

Length returns the length of the provided channel.

func Map

func Map[S any, T any](ch <-chan T, f func(v T) S) []S

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

func MapStream

func MapStream[S any, T any](ch <-chan T, f func(v T) S) <-chan S

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 Reduce

func Reduce[S any, T any](ch <-chan S, acc T, f func(S, T) T) T

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

func ReduceWithIndex

func ReduceWithIndex[S any, T any](ch <-chan S, acc T, f func(int, S, T) T) T

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 Take

func Take[T any](ch <-chan T, n int) []T

Take returns the first n items from the provided chan.

func ToChan

func ToChan[T any](items []T) <-chan T

ToChan returns a channel using the provided slice.

func ToSlice

func ToSlice[T any](ch <-chan T) []T

ToSlice converts the channel values to a 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.

func Zip2

func Zip2[S any, T any, KV any](sCh <-chan S, tCh <-chan T, f func(S, T) KV) <-chan KV

Zip2 zips corresponding elements from different types into a channel.

The zipping finishes as soon as either channel 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.

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

Number has the "+" operator.

Jump to

Keyboard shortcuts

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