Documentation ¶
Overview ¶
This package provides generic iterators and iterator transformations in go.
Iterators defer iteration execution by pushing iteration logic into the Next() function.
This package has a dependency on [go-types](https://github.com/patrickhuber/go-types) for the Option[T any] type. Users of the iter module can utilize Result[T] to capture errors or perform transformations on slices that can contain errors.
Index ¶
- func Async[T any](it Iterator[T], options ...ChannelOption[T]) chan T
- func Count[T any](iterator Iterator[T]) int
- func ForEach[T any](iterator Iterator[T], action func(t T))
- func ForEachIndex[T any](iterator Iterator[T], action func(i int, t T))
- func ToSlice[T any](iterator Iterator[T]) []T
- type ChannelOption
- type Iterator
- func FromChannel[T any](ch chan T, options ...ChannelOption[T]) Iterator[T]
- func FromMap[TKey comparable, TValue any](m map[TKey]TValue) Iterator[types.Tuple2[TKey, TValue]]
- func FromMapAsync[TKey comparable, TValue any](m map[TKey]TValue, cx context.Context) Iterator[types.Tuple2[TKey, TValue]]
- func FromSlice[T any](slice []T) Iterator[T]
- func Range[T constraints.Integer](begin T, end T) Iterator[T]
- func Repeat[T any](element T, count int) Iterator[T]
- func Runes(str string, options ...ChannelOption[rune]) Iterator[rune]
- func Select[TSource, TTarget any](iterator Iterator[TSource], transform func(TSource) TTarget) Iterator[TTarget]
- func Where[T any](iterator Iterator[T], where func(t T) bool) Iterator[T]
- func Zip[T1, T2 any](iter1 Iterator[T1], iter2 Iterator[T2]) Iterator[types.Tuple2[T1, T2]]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Async ¶ added in v0.2.2
func Async[T any](it Iterator[T], options ...ChannelOption[T]) chan T
Async returns a channel for the given iterator over the context. If the context is canceled, the Async function will return immediately.
func ForEachIndex ¶
Types ¶
type ChannelOption ¶
type ChannelOption[T any] func(*channelOption[T])
func WithContext ¶
func WithContext[T any](cx context.Context) ChannelOption[T]
WithContext provides an context.Context for channel operations
type Iterator ¶
type Iterator[T any] interface { Next() types.Option[T] }
Iterator defines a generic iterface for iterating over a sequence
func FromChannel ¶
func FromChannel[T any](ch chan T, options ...ChannelOption[T]) Iterator[T]
func FromMap ¶
func FromMap[TKey comparable, TValue any](m map[TKey]TValue) Iterator[types.Tuple2[TKey, TValue]]
FromMap returns an iterator over the given map. FromMap captures keys of the map in the first call to `Next()`. Subsequent calls to `Next()` track an index into the key slice to return the next key value pair. Each call to `Next()` returns an Option tuple of Key Value pairs.
func FromMapAsync ¶ added in v0.2.1
func FromMapAsync[TKey comparable, TValue any](m map[TKey]TValue, cx context.Context) Iterator[types.Tuple2[TKey, TValue]]
FromMapAsync returns a call to FromChannel with a channel created by iterating over the map in a go routine The context passed in is used to pass the WithContext chanel option to the FromChannel call
func Range ¶
func Range[T constraints.Integer](begin T, end T) Iterator[T]
func Repeat ¶ added in v0.2.0
Repeat returns an iterator that repeats the given `element` `count` times