iter

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: MIT Imports: 2 Imported by: 6

README

Iter

Coverage Go Report Card

Iterator and utilities.

Some inspiration from ReactiveX.

Examples

Iterating

Iterate over an iterator

package main

import (
    "fmt"

    "github.com/EliCDavis/iter"
)

func main() {
    arr := iter.Array([]int{1, 2, 3, 4, 5})

    sum := 0
    for {
        val, err := arr.Next()
        if err != nil {
            break
        }
        sum += val
    }

    // Prints: 15
    fmt.Println(sum)
}

Mapping

Map an iterator of one type to an iterator of a different type.

package main

import (
    "fmt"
    "strconv"

    "github.com/EliCDavis/iter"
    "github.com/EliCDavis/iter/iterops"
)

func main() {
    arr := iter.Array([]string{"1", "2", "3", "4", "5"})

    mapper := iterops.Map[string, int](&arr, func(s string) int {
        parsed, err := strconv.Atoi(s)
        if err != nil {
            panic(err)
        }
        return parsed
    })

    // Prints: [1 2 3 4 5]
    fmt.Println(iter.ReadFull(mapper))
}
Range

Range is a bound iterator that emits values incrementally starting from 0 and increasing to n-1, with n being the value specified in the Range constructor.

package main

import (
    "fmt"

    "github.com/EliCDavis/iter"
)

func main() {
    // Prints: 10 (0 + 1 + 2 + 3 + 4)
    fmt.Println(iter.Sum(iter.Range(5)))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAtEnd = errors.New("iterator is at the end")

Error to indicate the iterator is at the end of it's sequence.

View Source
var ErrInvalidRangeValue = errors.New("range iterator can not be constructed with values less than 0")

Error to indicate the value passed into the Range iterator is invalid.

View Source
var ErrNotInBounds = errors.New("index is outside of iterator bounds")

Error to indicate that the bound iterator is attempting to have an element accessed that is out of it's bounds.

Functions

func Average added in v1.0.2

func Average[T Number](i Iterator[T]) float64

func Max added in v1.0.2

func Max[T Number](i Iterator[T]) T

func Min added in v1.0.2

func Min[T Number](i Iterator[T]) T

func ReadFull

func ReadFull[T any](i Iterator[T]) []T

func Sum added in v1.0.2

func Sum[T Number](i Iterator[T]) T

Types

type ArrayIterator

type ArrayIterator[T any] struct {
	// contains filtered or unexported fields
}

func Array

func Array[T any](data []T) *ArrayIterator[T]

func (ArrayIterator[T]) At

func (i ArrayIterator[T]) At(index int) T

func (ArrayIterator[T]) Current

func (i ArrayIterator[T]) Current() T

func (ArrayIterator[T]) Len

func (i ArrayIterator[T]) Len() int

func (*ArrayIterator[T]) Next

func (i *ArrayIterator[T]) Next() (item T, err error)

func (*ArrayIterator[T]) Reset

func (i *ArrayIterator[T]) Reset()

type BoundIterator

type BoundIterator[T any] interface {
	Iterator[T]
	At(index int) T
	Len() int
	Reset()
}

type Iterator

type Iterator[T any] interface {
	Next() (T, error)
}

type Number added in v1.0.2

type Number interface {
	float32 | float64 | int | int32 | int64
}

type RangeIterator added in v1.0.2

type RangeIterator struct {
	// contains filtered or unexported fields
}

func Range added in v1.0.2

func Range(size int) *RangeIterator

func (RangeIterator) At added in v1.0.2

func (ri RangeIterator) At(index int) int

func (RangeIterator) Current added in v1.0.2

func (ri RangeIterator) Current() int

func (RangeIterator) Len added in v1.0.2

func (ri RangeIterator) Len() int

func (*RangeIterator) Next added in v1.0.2

func (ri *RangeIterator) Next() (item int, err error)

func (*RangeIterator) Reset added in v1.0.2

func (ri *RangeIterator) Reset()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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