iter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: MIT Imports: 5 Imported by: 0

README

Iteration library in go

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 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.

Range

The Range function creates an iterator over [begin..end) (inclusive begin, exclusive end)

rng := iter.Range(0, 10)
iter.ForEachIndex(rng, func(index int, i int) {
    if index > 0 {
        fmt.Print(" ")
    }
    fmt.Print(i)
})
// prints:
// 0 1 2 3 4 5 6 7 8 9

Select

The Select function transforms an iterator of one type to an iterator of another type.

rng := iter.Range(0,10)
strRng := iter.Select(rng, strconv.Itoa)
iter.ForEachIndex(strRng, func(index int, s string){
    if index > 0{
        fmt.Print(" ")
    }
    fmt.Print("'%s'", s)
})
// prints : '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'

Where

The Where function removes items from an iterator using a predicate function.

rng := iter.Range(0,10)
even := func(i int){ return i % 2 }
evens := iter.Where(rng, even)
iter.ForEachIndex(evens, func(index int, i int){
    if index > 0{
        fmt.Prin(" ")
    }
    fmt.Print("%d", i)
})
// prints : 0 2 4 6 8

FromSlice

The FromSlice function returns a slice iterator over the given slice

slice := []int{1, 3, 5, 7, 9}
it := iter.FromSlice(slice)
iter.ForEachIndex(it, func(index int, i int){
    if index > 0{
        fmt.Prin(" ")
    }
    fmt.Print("%d", i)
})
// prints 1 3 5 7 9

ToSlice

The ToSlice function returns a slice from the given iterator by iterating over all elements.

rng := iter.Range(0, 10)
slice := iter.ToSlice(rng)
fmt.Println(slice)
// prints : [0 1 2 3 4 5 6 7 8 9]

FromMap

The FromMap function 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.

m := map[string]int{"0":0, "1":1, "2":2, "3":3}
it := iter.FromMap(m)
fmt.Print("[ ")
iter.ForEachIndex(it, func(index int, tup types.Tuple2[string, int]){
    if index > 0{
        fmt.Print(", ")
    }    
    k, v := tup.Deconstruct()
    fmt.Print("'%s':%d", k, v)
})
fmt.Print(" ]")
// prints : [ '0':0, '1':1, '2':2, '3':3 ]

Count

The Count function returns the count of element in the iterator by iterating over all the elements.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count[T any](iterator Iterator[T]) int

func ForEach

func ForEach[T any](iterator Iterator[T], action func(t T))

func ForEachIndex

func ForEachIndex[T any](iterator Iterator[T], action func(i int, t T))

func ToSlice

func ToSlice[T any](iterator Iterator[T]) []T

Types

type ChannelOption

type ChannelOption[T any] func(*channelIterator[T])

func WithContext

func WithContext[T any](cx context.Context) ChannelOption[T]

type Iterator

type Iterator[T any] interface {
	Next() types.Option[T]
}

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]]

func FromSlice

func FromSlice[T any](slice []T) Iterator[T]

func Range

func Range[T constraints.Integer](begin T, end T) Iterator[T]

func Select

func Select[TSource, TTarget any](iterator Iterator[TSource], transform func(TSource) TTarget) Iterator[TTarget]

func Where

func Where[T any](iterator Iterator[T], where func(t T) bool) Iterator[T]

Jump to

Keyboard shortcuts

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