modulepackage
0.1.0
Repository: https://github.com/go-playground/itertools.git
Documentation: pkg.go.dev
# README
itertools
Go Iteration tools with a rusty flavour
Requirements
- Go 1.18+
Motivation
- I missed some iteration style tools in Rust.
- Wanted to see how far I could push Go generics(turns out it is very limited).
- For fun.
Example Usage (Simple)
package main
import (
"fmt"
"github.com/go-playground/itertools"
)
func main() {
results := itertools.WrapSlice([]int{4, 3, 2, 1, 0}).Iter().Filter(func(v int) bool {
if v >= 5 {
return true
}
return false
}).Collect()
fmt.Printf("%#v\n", results)
}
Example Usage (Complex)
See examples for more complex usages.
package main
import (
"fmt"
"github.com/go-playground/itertools"
optionext "github.com/go-playground/pkg/v5/values/option"
"strconv"
)
type FakeIterator struct {
max int
}
func (f *FakeIterator) Next() optionext.Option[int] {
f.max--
if f.max < 0 {
return optionext.None[int]()
}
return optionext.Some(f.max)
}
func main() {
results := itertools.WrapSliceMap[int, string]([]int{4, 3, 2, 1, 0}).Iter().Chain(&FakeIterator{
max: 10,
}).Filter(func(v int) bool {
if v >= 5 {
return true
}
return false
}).StepBy(2).Take(6).Map(func(v int) string {
return strconv.Itoa(v)
}).Iter().Collect()
fmt.Printf("%#v\n", results)
}
Caveats
Map
and it'sMAP
type parameter must be defined and passed around to be able to using inline. This is a limitation of Go generics not allowing new type parameters on methods.Chunk
can only be used at the end of a series of iterators fromIter
but can be used and wrapped byIter
again. This is a limitation of the Go Compiler which causes a recursive initialization issue https://github.com/golang/go/issues/50215.Iter
must be called on some types, like the wrapped slice or map types, to allow usage of helper functions tied directly to them but notIterate
Reduce
will have to be used directly instead of other helper function such asSum
,Product
, ... again because no new type parameters on methods.
How to Contribute
Make a pull request... can't guarantee it will be added, going to strictly vet what goes in.
License
Distributed under MIT License, please see license file within the code for more details.
# Functions
Chain creates a new `chainIterator[T]` for use.
ChainWithMap creates a new `chainIterator[T]` for use and parameter to specify a Map type for the `Iterate.Map` helper function.
Chunk creates a new `chunker` for use.
ChunkWithMap creates a new `chunker` for use that accepts a Map type for use with `Iterate`.
Filter creates a new `filterIterator`.
FilterWithMap creates a new `filterIterator` for use which also specifies a potential future `Map` operation.
Iter creates a new iterator with helper functions.
IterMap creates a new iterator with helper functions.
Map creates a new iterator for transformation of types.
Peekable accepts and `Iterator[T]` and turns it into a Peekable iterator.
StepBy returns a `stepByIterator[T]` for use.
StepByWithMap returns a `stepByIterator[T]` for use and can specify a future `Map` type conversion.
Take creates a new `takeIterator[T]` for use.
TakeWhile creates a new `takeWhileIterator[T,I]` for use.
TakeWhileWithMap creates a new `takeWhileIterator[T,I]` for use and can specify a future `Map` type conversion.
TakeWithMap creates a new `takeIterator[T]` for use and can specify a future `Map` type conversion.
WrapMap creates a new iterator for transformation of types.
WrapMapWithMap creates a new `mapWrapper` for use which also specifies a potential future `Map` operation.
WrapSlice accepts and turns a sliceWrapper into an iterator.
WrapSliceMap accepts and turns a sliceWrapper into an iterator with a map type specified for IterPar() to allow the Map helper function.
# Interfaces
Iterator is an interface representing something that iterates using the Next method.
PeekableIterator is an interface representing something that iterates using the Next method and ability to `Peek` the next element value without advancing the `Iterator`.
# Type aliases
FilterFn represents the `filterIterator` function.
MapFn represents the mapWrapper transformation function.
TakeWhileFn represents the `takeWhileIterator[T]` function.