GithubHelp home page GithubHelp logo

soft / iter Goto Github PK

View Code? Open in Web Editor NEW
71.0 3.0 5.0 13 KB

iter is a generic iterator library for Go

License: MIT License

Go 98.76% Nix 1.24%
go golang golang-library iterator iterators iteration generics optional

iter's Introduction

iter - Generic Iterators for Go ๐Ÿฆ„

License: MIT

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.

iter's People

Contributors

soft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.