iter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: MIT Imports: 1 Imported by: 3

README ¶

iter - Generic Iterators for Go 🦄

iter is a generic iterator library for Go 1.18 and greater. It should feel familiar to those familiar with Rust's Iterator trait.

Iterators

type Iterator[T any] interface {
        // Next yields a new value from the Iterator.
        Next() Option[T]
}

Iterator[T] represents an iterator yielding elements of type T.

Creating Iterators

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

Slice returns an Iterator that yields elements from a slice.

func String(input string) Iterator[rune]

String returns an Iterator yielding runes from the supplied string.

func Range(start, stop, step int) Iterator[int]

Range returns an Iterator over a range of integers.

func Func[T any](fn func() Option[T]) Iterator[T]

Func returns an Iterator from a function.

func Once[T any](value T) Iterator[T]

Once returns an Iterator that returns a value exactly once.

func Empty[T any]() Iterator[T]

Empty returns an empty Iterator.

func Repeat[T any](value T) Iterator[T]

Repeat returns an Iterator that repeatedly returns the same value.

Iterator Adapters

func Chain[T any](first Iterator[T], second Iterator[T]) Iterator[T]

Chain returns an Iterator that concatenates two iterators.

func Drop[T any](it Iterator[T], n uint) Iterator[T]

Drop returns an Iterator adapter that drops the first n items from the underlying Iterator before yielding any values.

func DropWhile[T any](it Iterator[T], pred func(T) bool) Iterator[T]

DropWhile returns an Iterator adapter that drops items from the underlying Iterator until pred predicate function returns true.

func Filter[T any](it Iterator[T], pred func(T) bool) Iterator[T]

Filter returns an Iterator adapter that yields elements from the underlying Iterator for which pred returns true.

func Flatten[T any](it Iterator[Iterator[T]]) Iterator[T]

Flatten returns an Iterator adapter that flattens nested iterators.

func Fuse[T any](it Iterator[T]) Iterator[T]

Fuse returns an Iterator adapter that will keep yielding None after the underlying Iterator has yielded None once.

func Map[T, R any](it Iterator[T], fn func(T) R) Iterator[R]

Map is an Iterator adapter that transforms each value yielded by the underlying iterator using fn.

func Take[T any](it Iterator[T], n uint) Iterator[T]

Take returns an Iterator adapter that yields the n first elements from the underlying Iterator.

func TakeWhile[T any](it Iterator[T], pred func(T) bool) Iterator[T]

TakeWhile returns an Iterator adapter that yields values from the underlying Iterator as long as pred predicate function returns true.

Consuming Iterators

func Count[T any](it Iterator[T]) uint

Count consumes an Iterator and returns the number of items it yielded.

func Fold[T any, B any](it Iterator[T], init B, fn func(B, T) B) B

Fold reduces Iterator using function fn.

func ForEach[T any](it Iterator[T], fn func(T))

ForEach consumes the Iterator applying fn to each yielded value.

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

ToSlice consumes an Iterator creating a slice from the yielded values.

func ToString(it Iterator[rune]) string

ToString consumes a rune Iterator creating a string.

func Find[T any](it Iterator[T], pred func(T) bool) Option[T]

Find the first element from Iterator that satisfies pred predicate function.

func All[T any](it Iterator[T], pred func(T) bool) bool

All tests if every element of the Iterator matches a predicate. An empty Iterator returns true.

func Any[T any](it Iterator[T], pred func(T) bool) bool

Any tests if any element of the Iterator matches a predicate. An empty Iterator returns false.

func Equal[T comparable](first Iterator[T], second Iterator[T]) bool

Determines if the elements of two Iterators are equal.

func EqualBy[T any](first Iterator[T], second Iterator[T], cmp func(T, T) bool) bool

Determines if the elements of two Iterators are equal using function cmp to compare elements.

func Nth[T any](it Iterator[T], n uint) Option[T]

Nth returns nth element of the Iterator.

Optional Values

type Option[T any] struct {
        // Has unexported fields.
}

Options[T] represents an optional value of type T.

func Some[T any](v T) Option[T]

Some returns an Option containing a value.

func None[T any]() Option[T]

None returns an empty Option.

func (opt Option[T]) IsSome() bool

IsSome returns true if Option contains a value.

func (opt Option[T]) IsNone() bool

IsNone returns true if Option is empty.

func (opt Option[T]) Unwrap() T

Unwrap extracts a value from Option. Panics if Option does not contain a value.

func (opt Option[T]) UnwrapOr(def T) T

UnwrapOr extracts a value from Option or returns a default value def if the Option is empty.

func (opt Option[T]) UnwrapOrElse(fn func() T) T

UnwrapOrElse extracts a value from Option or computes a value by calling fn if the Option is empty.

func MapOption[T any, R any](opt Option[T], fn func(T) R) Option[R]

MapOption applies a function fn to the contained value if it exists.

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func All ¶

func All[T any](it Iterator[T], pred func(T) bool) bool

All tests if every element of the Iterator matches a predicate. An empty Iterator returns true.

func Any ¶

func Any[T any](it Iterator[T], pred func(T) bool) bool

Any tests if any element of the Iterator matches a predicate. An empty Iterator returns false.

func Count ¶

func Count[T any](it Iterator[T]) uint

Count consumes an Iterator and returns the number of elements it yielded.

func Equal ¶

func Equal[T comparable](first Iterator[T], second Iterator[T]) bool

Determines if the elements of two Iterators are equal.

func EqualBy ¶

func EqualBy[T any](first Iterator[T], second Iterator[T], cmp func(T, T) bool) bool

Determines if the elements of two Iterators are equal using function cmp to compare elements.

func Fold ¶

func Fold[T any, B any](it Iterator[T], init B, fn func(B, T) B) B

Fold reduces Iterator using function fn.

func ForEach ¶

func ForEach[T any](it Iterator[T], fn func(T))

ForEach consumes the Iterator applying fn to each yielded value.

func ToSlice ¶

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

ToSlice consumes an Iterator creating a slice from the yielded values.

func ToString ¶

func ToString(it Iterator[rune]) string

ToString consumes a rune Iterator creating a string.

Types ¶

type Iterator ¶

type Iterator[T any] interface {
	// Next yields a new value from the Iterator.
	Next() Option[T]
}

Iterator[T] represents an iterator yielding elements of type T.

func Chain ¶

func Chain[T any](first Iterator[T], second Iterator[T]) Iterator[T]

Chain returns an Iterator that concatenates two iterators.

func Drop ¶

func Drop[T any](it Iterator[T], n uint) Iterator[T]

Drop returns an Iterator adapter that drops the first n elements from the underlying Iterator before yielding any values.

func DropWhile ¶

func DropWhile[T any](it Iterator[T], pred func(T) bool) Iterator[T]

DropWhile returns an Iterator adapter that drops elements from the underlying Iterator until pred predicate function returns true.

func Empty ¶

func Empty[T any]() Iterator[T]

Empty returns an empty Iterator.

func Filter ¶

func Filter[T any](it Iterator[T], pred func(T) bool) Iterator[T]

Filter returns an Iterator adapter that yields elements from the underlying Iterator for which pred returns true.

func Flatten ¶

func Flatten[T any](it Iterator[Iterator[T]]) Iterator[T]

Flatten returns an Iterator adapter that flattens nested iterators.

func Func ¶

func Func[T any](fn func() Option[T]) Iterator[T]

Func returns an Iterator from a function.

func Fuse ¶

func Fuse[T any](it Iterator[T]) Iterator[T]

Fuse returns an Iterator adapter that will keep yielding None after the underlying Iterator has yielded None once.

func Map ¶

func Map[T, R any](it Iterator[T], fn func(T) R) Iterator[R]

Map is an Iterator adapter that transforms each value yielded by the underlying iterator using fn.

func Once ¶

func Once[T any](value T) Iterator[T]

Once returns an Iterator that returns a value exactly once.

func Range ¶

func Range(start, stop, step int) Iterator[int]

Range returns an Iterator over a range of integers.

func Repeat ¶

func Repeat[T any](value T) Iterator[T]

Repeat returns an Iterator that repeatedly returns the same value.

func Slice ¶

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

Slice returns an Iterator that yields elements from a slice.

func String ¶

func String(input string) Iterator[rune]

String returns an Iterator yielding runes from the supplied string.

func Take ¶

func Take[T any](it Iterator[T], n uint) Iterator[T]

Take returns an Iterator adapter that yields the n first elements from the underlying Iterator.

func TakeWhile ¶

func TakeWhile[T any](it Iterator[T], pred func(T) bool) Iterator[T]

TakeWhile returns an Iterator adapter that yields values from the underlying Iterator as long as pred predicate function returns true.

type Option ¶

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

Options[T] represents an optional value of type T.

func Find ¶

func Find[T any](it Iterator[T], pred func(T) bool) Option[T]

Find the first element from Iterator that satisfies pred predicate function.

func MapOption ¶

func MapOption[T any, R any](opt Option[T], fn func(T) R) Option[R]

MapOption applies a function fn to the contained value if it exists.

func None ¶

func None[T any]() Option[T]

None returns an empty Option.

func Nth ¶

func Nth[T any](it Iterator[T], n uint) Option[T]

Nth returns nth element of the Iterator.

func Some ¶

func Some[T any](v T) Option[T]

Some returns an Option containing a value.

func (Option[T]) IsNone ¶

func (opt Option[T]) IsNone() bool

IsNone returns true if Option is empty.

func (Option[T]) IsSome ¶

func (opt Option[T]) IsSome() bool

IsSome returns true if Option contains a value.

func (Option[T]) Unwrap ¶

func (opt Option[T]) Unwrap() T

Unwrap extracts a value from Option. Panics if Option does not contain a value.

func (Option[T]) UnwrapOr ¶

func (opt Option[T]) UnwrapOr(def T) T

UnwrapOr extracts a value from Option or returns a default value def if the Option is empty.

func (Option[T]) UnwrapOrElse ¶

func (opt Option[T]) UnwrapOrElse(fn func() T) T

UnwrapOrElse extracts a value from Option or computes a value by calling fn if the Option is empty.

Jump to

Keyboard shortcuts

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