Gtools
Generic tools for go 1.18+
FT (func tools)
Provide func tools over iterators
Iterators for functions like Filter
, Map
, Reduce
, etc
solve 3 main problems:
- Does not allocate new slices (because you just iterates over provided one)
- Iterates over slice just once (without iterators in case chaining filter -> map -> reduce iterates 3 times [1 time in each function])
- Iterator can be writen for any type you needed. So you can work with custom types (such a
List
,Queue
,etc
) in same way
This package contains:
Functions:
Filter
- wrap provided iter and return new iterator, that yields elements satisfying the given functionMap
- wrap provided iter and return new iterator, that yields elements obtained by applying the given function to each element of the original iteratorSkip
- skip N iterations of iteratorChunk
- split provided iter into several slices returns iterator of slices with len less or equalsize
Scan
- same as reduce but instead of returning one result it return iterator of results at every stepProduct
- make cartesian product of input iters.Cycle
- return endless iterator that yields elements from original iterZip
- create a new iterator over provided 2. This iterator yields pairs of each iterator elements. It ends when one of iter ends (you can combine it if you need zip more than 2 iters:Zip(Zip(iter1, iter2), iter3)
)Enumerate
- returns an iterator of the original slice elements with numbering
Consumers:
Collect
- consumes iterator and return slice of its elementsCollectInto
- consumes the iterator and fill provided argumentsCollectR
- same asCollectInto
but this func returns value of provided (during call) type. This func uses reflect.Any
- consumes iter and returns true if any element of iter returns true on predicate func call on itAll
- consumes iter and returns true if all elements returns true on predicate func call on itReduce
- consumes iter, calls provided func on every element of iterator, accumulating result (has optional argument for inital value)Count
- consumes iter and count its elements (has optional argument to call only specific values)Sum
- consumes elements and return sum of it (can be used only on iterators with numeric types (such as int. float, complex)ForEach
- consumes iterator and apply provided function on itIntoChannel
- return channel that yields iterator elements (has optional arg context.Context)Max
- consumes iter and return max element find in it or nil if no such elementMin
- same asMax
but return min element
Iterator consctructors:
SliceIter
- iterator over sliceMapIter
- iterator over map (this iterator spawn goroutine to read from map) use this if you have huge size mapMapIterOverSlice
- iterator over map (this iterator creatingSliceIter
with all key-value pairs)