itlib

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: Apache-2.0 Imports: 4 Imported by: 3

Documentation

Overview

Package itlib provides generic function for operating on iterators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[T any](it itkit.Iterator[T], fn ApplyFn[T])

Apply walks through the given Iterator it and calls ApplyFn fn for every single entry.

func ApplyN

func ApplyN[T any](it itkit.Iterator[T], fn ApplyNFn[T])

ApplyN walks through the given Iterator it and calls ApplyNFn fn for every single entry together with its index.

func ApplyTo

func ApplyTo[T, R any](it itkit.Iterator[T], obj R, fn ApplyToFn[T, R]) R

ApplyTo walks through the given Iterator it and calls ApplyToFn fn for every single entry with the given value in obj of type R, returning the passed value.

ApplyTo behaves like ReduceWithInitial with the exception that the result of the callback for the previous iteration not being passed down to the next iteration.

func ChainI

func ChainI[T any](iters itkit.Iterator[itkit.Iterator[T]]) itkit.Iterator[T]

ChainI returns a ChainIterator chaining multiple Iterator iterators together.

func ChainV

func ChainV[T any](iters ...itkit.Iterator[T]) itkit.Iterator[T]

ChainV is the variadic version of ChainI.

func Each

func Each[T any](it itkit.Iterator[T], fn EachFn[T])

Each walks through the given Iterator it and calls EachFn fn for every single entry, aborting if EachFn fn returns true.

func EachN

func EachN[T any](it itkit.Iterator[T], fn EachNFn[T])

EachN walks through the given Iterator it and calls EachNFn fn for every single entry together with its index, aborting if the given function returns true.

func Empty

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

Empty returns an Iterator that is always exhausted.

func Filter

func Filter[T any](it itkit.Iterator[T], cb FilterFn[T]) itkit.Iterator[T]

Filter returns an Iterator yielding items from the given iterator for which the given FilterFn function returns true.

func Find

func Find[T any](it itkit.Iterator[T], fn EqualFn[T], needle T) (out T, ok bool)
func Head[T any](it itkit.Iterator[T]) (out T, ok bool)

Head returns the next value in the iterator and true, if the iterator has a next item, consuming it from the iterator as well. Returns the zero value and false otherwise.

func HeadOrElse added in v0.7.0

func HeadOrElse[T any](it itkit.Iterator[T], v T) T

HeadOrElse returns the next value in the iterator, if the iterator has a next item, consuming it from the iterator. Returns the provided default value otherwise.

func Map

func Map[T, V any](it itkit.Iterator[T], fn MapFn[T, V]) itkit.Iterator[V]

Map returns an iterator that applies MapFn function to every item of iterkit.Iterator iterable, yielding the results.

func Reduce

func Reduce[T, R any](it itkit.Iterator[T], fn AccumulatorFn[T, R]) (out R)

Reduce reduces the given Iterator to a value which is the accumulated result of running each value through AccumulatorFn, where each successive invocation of AccumulatorFn is supplied the return value of the previous invocation.

func ReduceWithInitial

func ReduceWithInitial[T, R any](initial R, it itkit.Iterator[T], fn AccumulatorFn[T, R]) (out R)

ReduceWithInitial reduces the given Iterator to a value which is the accumulated result of running each value through AccumulatorFn, where each successive invocation of AccumulatorFn is supplied the return value of the previous invocation.

func Sum

func Sum[T constraints.Ordered](it itkit.Iterator[T]) T

Sum accumulates the Iterator values based on the summation of their values.

func SumWithInitial

func SumWithInitial[T constraints.Ordered](initial T, it itkit.Iterator[T]) T

SumWithInitial accumulates the Iterator values based on the summation of their values.

func Zip

func Zip[T1, T2 any](it1 itkit.Iterator[T1], it2 itkit.Iterator[T2]) itkit.Iterator[Pair[T1, T2]]

Zip returns an iterator that aggregates elements from the given iterators.

The returned iterator yield T2 values, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator will stop when the shortest input iterable is exhausted.

Types

type AccumulatorFn

type AccumulatorFn[T, R any] func(R, T) R

type ApplyFn

type ApplyFn[T any] func(item T)

type ApplyNFn

type ApplyNFn[T any] func(i int, item T)

type ApplyToFn

type ApplyToFn[T, R any] func(obj R, item T)

type ChainIterator

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

ChainIterator chains multiple Iterator iterators together, traversing the given iterators until they are exhausted and proceeding with the next iterator.

func (*ChainIterator[T]) Next

func (c *ChainIterator[T]) Next() bool

func (*ChainIterator[T]) Value

func (c *ChainIterator[T]) Value() T

type EachFn

type EachFn[T any] func(item T) bool

type EachNFn

type EachNFn[T any] func(i int, item T) bool

type EmptyIterator

type EmptyIterator[T any] struct{}

func (EmptyIterator[T]) Next

func (e EmptyIterator[T]) Next() bool

func (EmptyIterator[T]) Value

func (e EmptyIterator[T]) Value() (v T)

type EqualFn

type EqualFn[T any] func(a, b T) bool

type FilterFn

type FilterFn[T any] func(element T) bool

type FilterIter

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

func (*FilterIter[T]) Next

func (f *FilterIter[T]) Next() bool

func (*FilterIter[T]) Value

func (f *FilterIter[T]) Value() T

type MapFn

type MapFn[T, V any] func(T) V

type MapIterator

type MapIterator[T, V any] struct {
	// contains filtered or unexported fields
}

func (*MapIterator[T, V]) Next

func (m *MapIterator[T, V]) Next() (ok bool)

func (*MapIterator[T, V]) Value

func (m *MapIterator[T, V]) Value() V

type Pair

type Pair[T1, T2 any] interface{ Values() (T1, T2) }

type ZipIterator

type ZipIterator[T1, T2 any] struct {
	Left  itkit.Iterator[T1]
	Right itkit.Iterator[T2]
	// contains filtered or unexported fields
}

func (*ZipIterator[T1, T2]) Next

func (it *ZipIterator[T1, T2]) Next() (ok bool)

func (*ZipIterator[T1, T2]) Value

func (it *ZipIterator[T1, T2]) Value() Pair[T1, T2]

Jump to

Keyboard shortcuts

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