iterator

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](iter Iterator[T], f func(x T) bool) bool

func Any

func Any[T any](iter Iterator[T], f func(x T) bool) bool

func Find

func Find[T any](iter Iterator[T], f func(x T) bool) (int, Option[T])

Find returns the element index and the element if it is found; otherwise returns (-1, None); Caller should check if the element "IsNone" before consuming the index.

func Fold

func Fold[T any, R any](iter Iterator[T], init R, f func(_acc R, _elem T) R) R

Fold is haskell's foldLeft It takes each element out of the iterator, apply a computation `f func(_acc R, _elem T) R` then update the init value; When there is no more element to process, it returns the init value as the final result.

func MapReduce

func MapReduce[T, R any](iter Iterator[T], init R, mapper func(x T) R, reducer func(R, R) R) R

func ParMapReduce

func ParMapReduce[T, R any](iter Iterator[T], init R, mapper func(x T) R, reducer func(R, R) R) R

func Partitioned

func Partitioned[T any](iter Iterator[T], f func(x T) bool) (Iterator[T], Iterator[T])

Partitioned has a flaw: if the L channel is blocked on write, the R channel cannot make progress either (can think of it as a dead lock); they must be buffered channels Should use select

Types

type Iterator

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

Iterator is essentially a generic read-channel with behaviors. The write-end of the channel must signal the termination by sending through a None value.

func ChunkSlice

func ChunkSlice[T any](iter Iterator[T], size int) Iterator[[]T]

ChunkSlice given an iterator that yields: 1, 2, 3, 4, 5 ...... n given a chunk size of p It produces a new iterator that yields a slice of p elements at a time (or less) from the original sequence: (1, 2, 3 ... p), (p+1, .... p+p), ...

func Concat

func Concat[T any](iterators ...Iterator[T]) Iterator[T]

Concat creates a new iterator from an arbitrary number of iterators of type Iterator[T], concatenating their elements

func Empty

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

func FlatMap

func FlatMap[T, R any](iter Iterator[T], mapper func(x T) Iterator[R]) Iterator[R]

func Flatten

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

func FromMap

func FromMap[K comparable, V any](m map[K]V) Iterator[Pair[K, V]]

func FromSlice

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

func FromValues added in v0.0.4

func FromValues[T any](xs ...T) Iterator[T]

func Intersperse

func Intersperse[T any](iter Iterator[T], sep T) Iterator[T]

Intersperse creates a new Iterator of the same type. It yields each element from the original iterator, inserting <sep> between two adjacent elements, e.g. given 1, 2, 3, 4 ... and sep = 100 it produces 1, 100, 2, 100, 3, 100, 4 ...

func Map

func Map[T any, R any](iter Iterator[T], f func(T) R) Iterator[R]

Map applies a function to each element and produces a new value; The results are yield in the new iterator.

func Once

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

func OnceWith

func OnceWith[T any](f func() T) Iterator[T]

func ParMap

func ParMap[T, R any](iter Iterator[T], f func(x T) R) Iterator[R]

ParMap respects the original order, but this causes significant overhead. If order is not important, use ParMapUnord instead. See parmap_test.go for a rough comparison between these two versions.

func ParMapUnord

func ParMapUnord[T, R any](iter Iterator[T], f func(x T) R) Iterator[R]

ParMapUnord disregard the order but can achieve better performance.

func Range

func Range(from int, to int) Iterator[int]

Range is exclusive from=1, to=100 results in a sequence of (1, 2, ... 99), 99 elements

func RangeInclusive

func RangeInclusive(first int, last int) Iterator[int]

func Repeat

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

func RepeatN

func RepeatN[T any](x T, num int) Iterator[T]

func Select added in v0.0.5

func Select[T any](buffer int64, iters ...Iterator[T]) Iterator[T]

func Successor

func Successor[T any](init Option[T], f func(T) Option[T]) Iterator[T]

Successor is copycat of Rust's successor function. It is an iterator-creator (or source). It repeatedly applies f to init (an Option[T]), yielding the result, till init becomes None. You can think of it as a series defined in the recursive form: Pn+1 = F( Pn )

func TailAppender

func TailAppender[T any](buffer int64) (Iterator[T], chan<- Option[T])

func WithIndex

func WithIndex[T any](iter Iterator[T]) Iterator[Pair[int64, T]]

func Zip

func Zip[T, P any](lhs Iterator[T], rhs Iterator[P]) Iterator[Pair[T, P]]

Zip takes two iterators (with element type T and P), returning a new iterator that yields Pair[T, P]. The number of pairs is determined by the shortest iterator, i.e. Zip(lhs, rhs).Count() == min(lhs.Count(), rhs.Count())

func (Iterator[T]) AdvanceBy

func (iter Iterator[T]) AdvanceBy(num int) Iterator[T]

AdvanceBy advances the iterator position by <num> which is equivalent to repeatedly calling Next() <num> times

func (Iterator[T]) Chain

func (iter Iterator[T]) Chain(other Iterator[T]) Iterator[T]

Chain is a simpler form of Concat: It concatenates itself and another iterator of the same type.

func (Iterator[T]) Count

func (iter Iterator[T]) Count() int

Count the number of elements

func (Iterator[T]) CountIf

func (iter Iterator[T]) CountIf(fi func(x T) bool) int

CountIf applies a filter function to each element. It counts the number of elements passing the check.

func (Iterator[T]) Cycle

func (iter Iterator[T]) Cycle() Iterator[T]

Cycle creates an infinite sequence using the given iterator; When it yields the last element from the original iterator, it repeats the sequence from the first element again.

func (Iterator[T]) Filter

func (iter Iterator[T]) Filter(f func(x T) bool) Iterator[T]

Filter applies a function to each element and gets a boolean value. If the value is true, the element is included in the resulting new iterator, otherwise it is discarded.

func (Iterator[T]) ForEach

func (iter Iterator[T]) ForEach(f func(T))

ForEach applies a function to each element and discard the result.

func (Iterator[T]) ForEachWithIndex added in v0.0.4

func (iter Iterator[T]) ForEachWithIndex(f func(int, T))

ForEachWithIndex adds the element index for the caller's convenience;

func (Iterator[T]) Last

func (iter Iterator[T]) Last() Option[T]

func (Iterator[T]) Next

func (iter Iterator[T]) Next() Option[T]

func (Iterator[T]) Reduce

func (iter Iterator[T]) Reduce(init T, f func(T, T) T) T

Reduce to apply f to each pair of elements concurrently and feed the result back to the input o o o o o o o o o o o .... ^^^ ^^^ ^^^ ^^^ ^^^

o   o   o   o   o
^^^^^   ^^^^^
  o       o
  ^^^^^^^^^
      o

func (Iterator[T]) Slice

func (iter Iterator[T]) Slice() []T

func (Iterator[T]) String

func (iter Iterator[T]) String(sep string) string

func (Iterator[T]) Tail

func (iter Iterator[T]) Tail() Iterator[T]

func (Iterator[T]) Take

func (iter Iterator[T]) Take(num int) Iterator[T]

Take is an iterator-filter that yields the first <num> elements; The iterator can terminate before it reaches <num>. It can be useful when dealing with an infinite series (Fibonacci series, the sequence of prime numbers, etc.)

type Pair

type Pair[T, P any] struct {
	First  T
	Second P
}

func NewPair

func NewPair[T, P any](x T, y P) Pair[T, P]

func (Pair[T, P]) Unpack

func (p Pair[T, P]) Unpack() (T, P)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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