Documentation ¶
Overview ¶
Package stream provides streaming functionality similar to java.util.stream
SPDX-License-Identifier: Apache-2.0
Index ¶
- func Abs[T constraint.SignedInteger](it iter.Iter[T]) iter.Iter[T]
- func AbsBigOps[T constraint.BigOps[T]](it iter.Iter[T]) iter.Iter[T]
- func AllMatch[T any](filter func(T) bool) func(iter.Iter[T]) iter.Iter[bool]
- func AnyMatch[T any](filter func(T) bool) func(iter.Iter[T]) iter.Iter[bool]
- func AvgBigOps[T constraint.BigOps[T]](it iter.Iter[T]) iter.Iter[T]
- func AvgInt[T constraint.SignedInteger](it iter.Iter[T]) iter.Iter[T]
- func AvgUint[T constraint.UnsignedInteger](it iter.Iter[T]) iter.Iter[T]
- func Count[T any](it iter.Iter[T]) iter.Iter[int]
- func Distinct[T comparable](it iter.Iter[T]) iter.Iter[T]
- func Duplicate[T comparable](it iter.Iter[T]) iter.Iter[T]
- func ExpandMaps[K comparable, V any](it iter.Iter[map[K]V]) iter.Iter[tuple.Two[K, V]]
- func ExpandSlices[T any](it iter.Iter[[]T]) iter.Iter[T]
- func Filter[T any](filter func(T) bool) func(iter.Iter[T]) iter.Iter[T]
- func Generator[T, U any](gen func() func(iter.Iter[T]) iter.Iter[U]) func(iter.Iter[T]) iter.Iter[U]
- func Limit[T any](n uint) func(iter.Iter[T]) iter.Iter[T]
- func Map[T, U any](mapper func(T) U) func(iter.Iter[T]) iter.Iter[U]
- func MapError[T, U any](mapper func(T) (U, error)) func(iter.Iter[T]) iter.Iter[U]
- func Max[T constraint.Ordered](it iter.Iter[T]) iter.Iter[T]
- func MaxCmp[T constraint.Cmp[T]](it iter.Iter[T]) iter.Iter[T]
- func Min[T constraint.Ordered](it iter.Iter[T]) iter.Iter[T]
- func MinCmp[T constraint.Cmp[T]](it iter.Iter[T]) iter.Iter[T]
- func NoneMatch[T any](filter func(T) bool) func(iter.Iter[T]) iter.Iter[bool]
- func Parallel[T, U any](transforms func(iter.Iter[T]) iter.Iter[U], info ...PInfo) func(iter.Iter[T]) iter.Iter[U]
- func Peek[T any](fn func(T)) func(iter.Iter[T]) iter.Iter[T]
- func Reduce[T any](reducer func(T, T) T, identity ...T) func(iter.Iter[T]) iter.Iter[T]
- func ReduceIntoSlice[T any](slc []T) func(iter.Iter[T]) iter.Iter[[]T]
- func ReduceTo[T, U any](reducer func(U, T) U, identity ...U) func(iter.Iter[T]) iter.Iter[U]
- func ReduceToBool[T any](predicate func(T) bool, identity bool, stopVal bool) func(iter.Iter[T]) iter.Iter[bool]
- func ReduceToMap[K comparable, V any](it iter.Iter[tuple.Two[K, V]]) iter.Iter[map[K]V]
- func ReduceToSlice[T any](it iter.Iter[T]) iter.Iter[[]T]
- func Reverse[T any](it iter.Iter[T]) iter.Iter[T]
- func Skip[T any](n uint) func(iter.Iter[T]) iter.Iter[T]
- func SortBy[T any](less func(T, T) bool) func(iter.Iter[T]) iter.Iter[T]
- func SortCmp[T constraint.Cmp[T]](it iter.Iter[T]) iter.Iter[T]
- func SortComplex[T constraint.Complex](it iter.Iter[T]) iter.Iter[T]
- func SortOrdered[T constraint.Ordered](it iter.Iter[T]) iter.Iter[T]
- func Sum[T constraint.IntegerAndFloat](it iter.Iter[T]) iter.Iter[T]
- func SumBigOps[T constraint.BigOps[T]](it iter.Iter[T]) iter.Iter[T]
- type PInfo
- type PUnit
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
func Abs[T constraint.SignedInteger](it iter.Iter[T]) iter.Iter[T]
Abs converts all elements into their absolute values. See MapError for error handling in cases where there is no corresponding value for a negative integer.
func AllMatch ¶
AllMatch reduces Iter[T] to an Iter[bool] with a single value that is true if the Iter[T] is empty or all elements pass the filter. Boolean short circuit logic stops on first case where filter returns false. Calls ReduceToBool(filter, true, false).
func AnyMatch ¶
AnyMatch reduces Iter[T] to an Iter[bool] with a single value that is true if the Iter[T] is non-empty and any element passes the filter. Boolean short circuit logic stops on first case where filter returns true. Calls ReduceToBool(filter, true, false).
func AvgBigOps ¶
AvgBigOps reduces all *big.Int, *big.Float, or *big.Rat elements in the input set to their average. If the input set is empty, the result is empty. The average is rounded only for *big.Int. See math.DivBigOps
func AvgInt ¶
func AvgInt[T constraint.SignedInteger](it iter.Iter[T]) iter.Iter[T]
AvgInt reduces all signed integer elements in the input set to their average. If the input set is empty, the result is empty. The average is rounded. See math.Add, math.Div.
func AvgUint ¶
func AvgUint[T constraint.UnsignedInteger](it iter.Iter[T]) iter.Iter[T]
AvgUint reduces all unsigned integer elements in the input set to their average. If the input set is empty, the result is empty. The average is rounded. See math.Add, math.Div.
func Count ¶
Count reduces Iter[T] to an Iter[int] with a single value that is the number of elements in the Iter[T].
func Distinct ¶
func Distinct[T comparable](it iter.Iter[T]) iter.Iter[T]
Distinct reduces Iter[T] to an Iter[T] with distinct values. Distinct is a stateful transform that has to track unique values across iterator Next and Value calls.
Distinct uses Generator internally to ensure what whenever a new Iter is encountered, a new state of an empty set of values is generated. This allows a composition to be stored in a variable and reused across data sets correctly.
If you want Distinct to have one state across multiple Iters, use Concat to create a single Iter that traverses them.
func Duplicate ¶
func Duplicate[T comparable](it iter.Iter[T]) iter.Iter[T]
Duplicate reduces Iter[T] to an Iter[T] with duplicate values. Like Distinct, a given duplicate will only appear once. The order of the elements is the order in which the second occurence of each value appears. Eg, for the input 1,2,2,1 the result is 2,1 since the second value of 2 appears before the second value of 1. See Distinct for an explanation of statefulness and the usage of Generator.
func ExpandMaps ¶
ExpandMaps is the opposite of ReduceToMap: an Iter[map[int]string] of {1: "1", 2: "2", 3: "3"} becomes an Iter[tuple.Two[int, string]] of {1: "1"}, {2: "2"}, {3: "3"]. If the source Iter contains multiple maps, they are combined together into one set of data (skipping nils), so that an Iter[map[int]string] of {1: "1", 2: "2"}, nil, {}, {3: "3"} also becomes an Iter[tuple.Two[int, string]] of {1: "1"}, {2: "2"}, {3: "3"}. An empty Iter or an Iter with nil/empty maps is expanded to an empty Iter.
func ExpandSlices ¶
ExpandSlices is the opposite of ReduceToSlice: an Iter[[]int] of [1,2,3,4,5] becomes an Iter[int] of 1,2,3,4,5. If the source Iter contains multiple slices, they are combined together into one set of data (skipping nil and empty slices), so that an Iter[[]int] of [1,2,3], nil, [], [4,5] also becomes an Iter[int] of 1,2,3,4,5. An empty Iter or an Iter with nil/empty slices is expanded to an empty Iter.
func Filter ¶
Filter constructs a new Iter[T] from an Iter[T] and a func that returns true if a T passes the filter.
The resulting iter can return any kind of error from source iter, or EOI.
func Generator ¶
func Generator[T, U any](gen func() func(iter.Iter[T]) iter.Iter[U]) func(iter.Iter[T]) iter.Iter[U]
Generator receives a generator (a func of no args that returns a func of Iter[T] -> Iter[U], and detects if the Iter[T] has changed address. If so, it internally generates a new function by invoking the generator.
This allows stateful transforms of Iter[T] -> Iter[U] that track state across calls to begin with a new initial state for each data set the transform is applied to.
Generator is not thread safe, so be careful about storing a composition containing a Generator in a global variable: 1. Declare the global variable as a function of no args that generates the composition when executed 2. Declare composition in a local variable so each thread makes its own copy 3. Store composition in a Context that is visible across methods in the thread
See Distinct for an example of a stateful function that uses Generator internally.
func Limit ¶
Limit returns the first n elements, then iteration stops and all further elements are ignored. If there fewer than n elements in total, then all n elements are returned.
Note that for the set 1,2,3,4,5 the composition of Skip(1),Limit(3) will first skip 1 then limit to 2,3,4; whereas the composition of Limit(3),Skip(1) will first limit to 1,2,3 then skip 1 returning 2,3.
func Map ¶
Map constructs a new Iter[U] from an Iter[T] and a func that transforms a T to a U.
The resulting iter can return any kind of error from source iter, or EOI.
func MapError ¶
MapError is similar to Map, except the mapper function returns (U, error), and the first element that returns a non-nil error results in iteration being cut short.
The resulting iter can return any kind of error from source iter, or EOI.
func Max ¶
Max calculates the maximum value of any primitive numeric type or string. If the input set is empty, the result is empty.
func MaxCmp ¶
MaxCmp calculates the maximum value of any type implementing the Cmp interface, which includes all the big types. If the input set is empty, the result is empty.
func Min ¶
Min calculates the minimum value of any primitive numeric type or string. If the input set is empty, the result is empty.
func MinCmp ¶
MaxCmp calculates the maximum value of any type implementing the Cmp interface, which includes all the big types. If the input set is empty, the result is empty.
func NoneMatch ¶
NoneMatch reduces Iter[T] to an Iter[bool] with a single value that is true if the Iter[T] is empty or no elements pass the filter. Boolean short circuit logic stops on first case where filter returns true. Calls ReduceToBool(!filter, true, false).
func Parallel ¶
func Parallel[T, U any](transforms func(iter.Iter[T]) iter.Iter[U], info ...PInfo) func(iter.Iter[T]) iter.Iter[U]
Parallel collects all the items of the source iter into a []T and divvies them up into buckets, then uses a set of threads, one per bucket, to process the items using the given set of transforms. If the optional PInfo is provided, The number N is interpreted in one of two ways, depending on the PUnit:
Threads: N is the number of threads, the bucket size for each thread is number of items / N, with remainder r
distributed across first r threads. If number of items <= N, a single thread is used.
Items: N is the bucket size, the number of threads is number of items / N, with remainder r handled by an
additional thread. If number of items <= N, a single thread is used.
If no PInfo is provided, the number of threads is the square root of the number of items, so that each thread has the same number of items - except for the last thread, which may have slightly less.
If the input has no items, an empty Iter is returned. If the input has one item, the transforms are performed in the same thread. If the input has two or more items, the above algorithm is used to perform transforms in two or more threads.
If types T and U are the same, then a single slice is allocated to contain the input and modified in place to produce the output. Otherwise, two slices are allocated, one for input and one for output.
func Reduce ¶
Reduce reduces all elements in the input set to an empty or single element output set, depending on two factors: - the number of elements in the input set (0, 1, or multiple) - whether or not the optional identity is provided
If the optional identity is NOT provided: - 0 elements: empty - 1 element: the element - multiple elements: reducer(reducer(reducer(first, second), third), ...)
If the optional identity IS provided:
- 0 elements: identity - 1 element: reducer(identity, the element) - multiple elements: reducer(reducer(reducer(identity, first), second), ...)
The resulting iter can return any kind of error from source iter, or EOI.
func ReduceIntoSlice ¶
ReduceIntoSlice is the same as ReduceToSlice, except that: - It accepts a target slice to append results to - It generates a transform
The generated transform panics if the target slice length is not at least as many elements as the source iter. If the underlying iter returns a non-nil non-EOI error, the provided slice will have zero values.
func ReduceTo ¶
ReduceTo is similar to Reduce, except that the result does not have to be the same type The resulting iter can return any kind of error from source iter, or EOI.
func ReduceToBool ¶
func ReduceToBool[T any]( predicate func(T) bool, identity bool, stopVal bool, ) func(iter.Iter[T]) iter.Iter[bool]
ReduceToBool is similar to ReduceTo, except that it uses boolean short circuit logic to stop iterating early if possible. If stopVal is true, then early termination occurs on the first call to reducer that returns true, else it occurs on the first call to reducer that returns false.
func ReduceToMap ¶
ReduceToMap reduces an Iter[tuple.Two[K, V]]] into a Iter[map[K, V]] that contains a single element if type map[K, V]. Eg, an Iter[tuple.Two[int]string] of {1: "1"}, {2: "2"} becomes an Iter[map[int]string] of {1: "1", 2: "2"}. If multiple tuple.Two objects in the Iter have the same key, the last such object in iteration order determines the value for the key in the resulting map. An empty Iter is reduced to an empty map.
func ReduceToSlice ¶
ReduceToSlice reduces an Iter[T] into a Iter[[]T] that contains a single element of type []T. Eg, an Iter[int] of 1,2,3,4,5 becomes an Iter[[]int] of [1,2,3,4,5]. An empty Iter is reduced to a zero length slice.
func Skip ¶
Skip skips the first n elements, then iteration continues from there. If there are n or fewer elements in total, then the resulting iter is empty.
Note that for the set 1,2,3,4,5 the composition os Skip(1),Limit(3) will first skip 1 then limit to 2,3,4; whereas the composition of Limit(3),Skip(1) will first limit to 1,2,3 then skip 1 returning 2,3.
func SortBy ¶
SortBy sorts any type using funcs.SliceSortBy and the given comparator. The input iter must have a finite size.
func SortCmp ¶
SortCmp sorts a Cmp type using funcs.SliceSortCmp. The input iter must have a finite size.
func SortComplex ¶
SortComplex sorts a Complex type using funcs.SliceSortComplex. The input iter must have a finite size.
func SortOrdered ¶
SortOrdered sorts an Ordered type that is implicitly sortable using funcs.SliceSortOrdered. The input iter must have a finite size.
func Sum ¶
func Sum[T constraint.IntegerAndFloat](it iter.Iter[T]) iter.Iter[T]
Sum reduces all elements in the input set to their sum. If the input set is empty, the result is empty.