iter

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllOf

func AllOf[T any](iter Iter[T], predicate func(T) bool) bool

AllOf tests if every element of the iterator matches a predicate.

allOf() takes a closure that returns true or false. It applies this closure to each element of the iterator, and if they all return true, then so does allOf(). If any of them return false, it returns false.

allOf() is short-circuiting; in other words, it will stop processing as soon as it finds a false, given that no matter what else happens, the result will also be false.

An empty iterator returns true.

Examples:

s := []int{1, 2, 3}
assertTrue(iter.AllOf(slices.Iter(s)), func(x int) bool { return x < 10 })
assertFalse(iter.AllOf(slices.Iter(s)), func(x int) bool { return x > 100 })

func AnyOf

func AnyOf[T any](iter Iter[T], predicate func(T) bool) bool

AnyOf tests if any element of the iterator matches a predicate.

anyOf() takes a closure that returns true or false. It applies this closure to each element of the iterator, and if any of them return true, then so does any(). If they all return false, it returns false.

anyOf() is short-circuiting; in other words, it will stop processing as soon as it finds a true, given that no matter what else happens, the result will also be true.

An empty iterator returns false.

Examples:

s := []int{1, 2, 3}
assertTrue(iter.Any(slices.Iter(s)), func(x int) bool { return x%2 == 0 })
assertFalse(iter.Any(slices.Iter(s)), func(x int) bool { return x > 100 })

func Cmp

func Cmp[T cmp.Ordered](iter Iter[T], other Iter[T]) int

func CmpBy

func CmpBy[T any, U any](iter Iter[T], other Iter[U], cmp func(T, U) int) int

func CmpByKey

func CmpByKey[T any, K cmp.Ordered](iter Iter[T], other Iter[T], keyFn func(T) K) int

func CollectInto

func CollectInto[T any, E Extend[T]](iter Iter[T], collection E) E

func CollectToSlice

func CollectToSlice[T any](iter Iter[T]) []T

func Count

func Count[T any](iter Iter[T]) int

func CountBy

func CountBy[T any](iter Iter[T], predicate func(T) bool) int

func Equal

func Equal[T comparable](iter Iter[T], other Iter[T]) bool

func EqualBy

func EqualBy[T, U any](iter Iter[T], other Iter[U], eq func(T, U) bool) bool

func EqualByKey

func EqualByKey[T any, K comparable](iter Iter[T], other Iter[T], keyFn func(T) K) bool

func FindLeft

func FindLeft[T any](iter Iter[T], f func(T) bool) optional.Optional[T]

func FindMap

func FindMap[T, U any](iter Iter[T], f func(T) optional.Optional[U]) optional.Optional[U]

func FindRight

func FindRight[T any](iter PrevIter[T], f func(T) bool) optional.Optional[T]

func FoldLeft

func FoldLeft[T any, A any](iter Iter[T], init A, f func(A, T) A) A

func FoldRight

func FoldRight[T any, A any](iter PrevIter[T], init A, f func(A, T) A) A

func ForEach

func ForEach[T any](iter Iter[T], f func(T))

func IsSorted

func IsSorted[T cmp.Ordered](iter Iter[T]) bool

func IsSortedBy

func IsSortedBy[T any](iter Iter[T], cmp func(T, T) int) bool

func IsSortedByKey

func IsSortedByKey[T any, K cmp.Ordered](iter Iter[T], keyFn func(T) K) bool

func Max

func Max[T cmp.Ordered](iter Iter[T]) optional.Optional[T]

func MaxBy

func MaxBy[T any](iter Iter[T], cmp func(T, T) int) optional.Optional[T]

func MaxByKey

func MaxByKey[T any, K cmp.Ordered](iter Iter[T], keyFn func(T) K) optional.Optional[T]

func Min

func Min[T cmp.Ordered](iter Iter[T]) optional.Optional[T]

func MinBy

func MinBy[T any](iter Iter[T], cmp func(T, T) int) optional.Optional[T]

func MinByKey

func MinByKey[T any, K cmp.Ordered](iter Iter[T], keyFn func(T) K) optional.Optional[T]

func MinMax

func MinMax[T cmp.Ordered](iter Iter[T]) optional.Optional[tuple.Pair[T, T]]

func MinMaxBy

func MinMaxBy[T any](iter Iter[T], cmp func(T, T) int) optional.Optional[tuple.Pair[T, T]]

func MinMaxByKey

func MinMaxByKey[T any, K cmp.Ordered](iter Iter[T], keyFn func(T) K) optional.Optional[tuple.Pair[T, T]]

func NoneOf

func NoneOf[T any](iter Iter[T], predicate func(T) bool) bool

NoneOf tests if none element of the iterator matches a predicate.

noneOf() takes a closure that returns true or false. It applies this closure to each element of the iterator, and if none of them return true, then so does none(). If they all return false, it returns true.

An empty iterator returns true.

Examples:

s := []int{1, 2, 3}
assertTrue(iter.None(slices.Iter(s)), func(x int) bool { return x%2 == 0 })
assertFalse(iter.None(slices.Iter(s)), func(x int) bool { return x > 100 })

func Nth

func Nth[T any](iter Iter[T], n int) optional.Optional[T]

func Partition

func Partition[T any](iter Iter[T], partition func(T) bool) (Iter[T], Iter[T])

func PositionLeft

func PositionLeft[T any](iter Iter[T], f func(T) bool) optional.Optional[int]

PositionLeft searches for an element in an iterator, returning its index.

PositionLeft() takes a closure that returns true or false. It applies this closure to each element of the iterator, and if one of them returns true, then PositionLeft() returns Some(index). If all of them return false, it returns None.

PositionLeft() is short-circuiting; in other words, it will stop processing as soon as it finds a true.

func ReduceLeft

func ReduceLeft[T any](iter Iter[T], f func(T, T) T) optional.Optional[T]

func ReduceRight

func ReduceRight[T any](iter PrevIter[T], f func(T, T) T) optional.Optional[T]

func ToString

func ToString[T fmt.Stringer](iter Iter[T], sep string) string

func ToStringBy

func ToStringBy[T any](iter Iter[T], sep string, f func(T) string) string

func UnzipBy

func UnzipBy[A, B, C any](iter Iter[A], unzip func(A) (B, C)) (Iter[B], Iter[C])

Types

type AllOfIter

type AllOfIter[T any] interface {
	Iter[T]
	AllOf(predicate func(T) bool) bool
}

type AnyOfIter

type AnyOfIter[T any] interface {
	Iter[T]
	AnyOf(predicate func(T) bool) bool
}

type CmpByIter

type CmpByIter[T any, U any] interface {
	Iter[T]
	CmpBy(other Iter[U], cmp func(T, U) int) int
}

type CmpByKeyIter

type CmpByKeyIter[T any, K cmp.Ordered] interface {
	Iter[T]
	CmpByKey(other Iter[T], keyFn func(T) K) int
}

type CmpIter

type CmpIter[T cmp.Ordered] interface {
	Iter[T]
	Cmp(other Iter[T]) int
}

type CountByIter

type CountByIter[T any] interface {
	Iter[T]
	CountBy(predicate func(T) bool) int
}

type CountIter

type CountIter[T any] interface {
	Iter[T]
	Count() int
}

type EnumerateIter

type EnumerateIter[T any] interface {
	Iter[T]
	Enumerate() Iter[tuple.Pair[int, T]]
}

type EqualByIter

type EqualByIter[T, U any] interface {
	Iter[T]
	EqualBy(other Iter[U], eq func(T, U) bool) bool
}

type EqualByKeyIter

type EqualByKeyIter[T any, K comparable] interface {
	Iter[T]
	EqualByKey(other Iter[T], keyFn func(T) K) bool
}

type EqualIter

type EqualIter[T comparable] interface {
	Iter[T]
	Equal(other Iter[T]) bool
}

type Extend

type Extend[T any] interface{ Extend(iter Iter[T]) }

type FilterIter

type FilterIter[T any] interface {
	Iter[T]
	Filter(f func(T) bool) Iter[T]
}

type FilterMapIter

type FilterMapIter[T, U any] interface {
	Iter[T]
	FilterMap(f func(T) optional.Optional[U]) Iter[U]
}

type FindLeftIter

type FindLeftIter[T any] interface {
	Iter[T]
	FindLeft(f func(T) bool) optional.Optional[T]
}

type FindMapIter

type FindMapIter[T, U any] interface {
	Iter[T]
	FindMap(f func(T) optional.Optional[U]) optional.Optional[U]
}

type FindRightIter

type FindRightIter[T any] interface {
	Iter[T]
	FindRight(f func(T) bool) optional.Optional[T]
}

type FoldLeftIter

type FoldLeftIter[T any, A any] interface {
	Iter[T]
	FoldLeft(init A, f func(A, T) A) A
}

type FoldRightIter

type FoldRightIter[T any, A any] interface {
	PrevIter[T]
	FoldRight(init A, f func(A, T) A) A
}

type ForEachIter

type ForEachIter[T any] interface {
	Iter[T]
	ForEach(f func(T))
}

type IsSortedByIter

type IsSortedByIter[T any] interface {
	Iter[T]
	IsSortedBy(cmp func(T, T) int) bool
}

type IsSortedByKeyIter

type IsSortedByKeyIter[T any, K cmp.Ordered] interface {
	Iter[T]
	IsSortedByKey(keyFn func(T) K) bool
}

type IsSortedIter

type IsSortedIter[T cmp.Ordered] interface {
	Iter[T]
	IsSorted() bool
}

type Iter

type Iter[T any] interface {
	// Next advances the iter and returns the next value.
	//
	// Returns [optional.None] when iteration is finished.
	//
	// Example:
	//  iter := slices.Iter(1, 2, 3)
	//  iter.Next() // Some(1)
	//  iter.Next() // Some(2)
	//  iter.Next() // Some(3)
	//  iter.Next() // None
	Next() optional.Optional[T]
}

Iter is the interface that wraps the basic methods for iterating over a collection.

💣 Important: Iterators usually contains state, so the behavior of reuse an Iter is undefined.

func Chain

func Chain[T any](iter Iter[T], other Iter[T]) Iter[T]

func Counter

func Counter() Iter[int]

Counter return an endless counter Iter start from 0

func Empty

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

Empty create an Iter always return None.

func Enumerate

func Enumerate[T any](iter Iter[T]) Iter[tuple.Pair[int, T]]

func Filter

func Filter[T any](iter Iter[T], f func(T) bool) Iter[T]

func FilterMap

func FilterMap[T, U any](iter Iter[T], f func(T) optional.Optional[U]) Iter[U]

func Map

func Map[T, U any](iter Iter[T], f func(T) U) Iter[U]

func MapWhile

func MapWhile[T, U any](iter Iter[T], f func(T) optional.Optional[U]) Iter[U]

func OfChan

func OfChan[T any](ch <-chan T) Iter[T]

OfChan create a new Iter[T] from chan T

func Range

func Range(start int, end int) Iter[int]

Range returns an Iter that iterate over [start, end) when end <= start, it panics

func Repeat

func Repeat[T any](v T) Iter[T]

Repeat create an Iter always return the same value.

func Rev

func Rev[T any](iter PrevIter[T]) Iter[T]

func Skip

func Skip[T any](iter Iter[T], n int) Iter[T]

func StepBy

func StepBy[T any](iter Iter[T], step int) Iter[T]

func Successor

func Successor[T any](init T, successor func(T) optional.Optional[T]) Iter[T]

func Take

func Take[T any](iter Iter[T], n int) Iter[T]

func Zip

func Zip[A, B any](iter Iter[A], other Iter[B]) Iter[tuple.Pair[A, B]]

func ZipBy

func ZipBy[A, B, C any](iter Iter[A], other Iter[B], f func(A, B) C) Iter[C]

type IterFunc

type IterFunc[T any] func() optional.Optional[T]

IterFunc is a function type that implements Iter.

func (IterFunc[T]) Next

func (fn IterFunc[T]) Next() optional.Optional[T]

type Iterable

type Iterable[T any] interface {
	Iter() Iter[T]
}

type MapIter

type MapIter[T, U any] interface {
	Iter[T]
	Map(f func(T) U) Iter[U]
}

type MapWhileIter

type MapWhileIter[T, U any] interface {
	Iter[T]
	MapWhile(f func(T) optional.Optional[U]) Iter[U]
}

type MaxByIter

type MaxByIter[T any] interface {
	Iter[T]
	MaxBy(cmp func(T, T) int) optional.Optional[T]
}

type MaxByKeyIter

type MaxByKeyIter[T any, K cmp.Ordered] interface {
	Iter[T]
	MaxByKey(keyFn func(T) K) optional.Optional[T]
}

type MaxIter

type MaxIter[T cmp.Ordered] interface {
	Iter[T]
	Max() optional.Optional[T]
}

type MinByIter

type MinByIter[T any] interface {
	Iter[T]
	MinBy(cmp func(T, T) int) optional.Optional[T]
}

type MinByKeyIter

type MinByKeyIter[T any, K cmp.Ordered] interface {
	Iter[T]
	MinByKey(keyFn func(T) K) optional.Optional[T]
}

type MinIter

type MinIter[T any] interface {
	Iter[T]
	Min() optional.Optional[T]
}

type MinMaxByIter

type MinMaxByIter[T any] interface {
	Iter[T]
	MinMaxBy(cmp func(T, T) int) optional.Optional[tuple.Pair[T, T]]
}

type MinMaxByKeyIter

type MinMaxByKeyIter[T any, K cmp.Ordered] interface {
	Iter[T]
	MinMaxByKey(keyFn func(T) K) optional.Optional[tuple.Pair[T, T]]
}

type MinMaxIter

type MinMaxIter[T cmp.Ordered] interface {
	Iter[T]
	MinMax() optional.Optional[tuple.Pair[T, T]]
}

type NoneOfIter

type NoneOfIter[T any] interface {
	Iter[T]
	NoneOf(predicate func(T) bool) bool
}

type PartitionIter

type PartitionIter[T any] interface {
	Iter[T]
	Partition(partition func(T) bool) (Iter[T], Iter[T])
}

type PositionLeftIter

type PositionLeftIter[T any] interface {
	Iter[T]
	PositionLeft(f func(T) bool) optional.Optional[int]
}

type PrevIter

type PrevIter[T any] interface {
	Iter[T]
	Prev() optional.Optional[T]
}

PrevIter is the interface that wraps the basic methods for iterating over a collection in both directions.

func OfSlice

func OfSlice[T any](slice []T) PrevIter[T]

OfSlice create a new Iter[T] from sequence of T.

func OfSliceVariadic

func OfSliceVariadic[T any](elems ...T) PrevIter[T]

OfSliceVariadic create a new Iter[T] from variadic sequence of T

type PrevIterable

type PrevIterable[T any] interface {
	PrevIter() PrevIter[T]
}

type SkipIter

type SkipIter[T any] interface {
	Iter[T]
	Skip(n int) Iter[T]
}

type SkipWhileIter

type SkipWhileIter[T any] interface {
	Iter[T]
	SkipWhile(skip func(T) bool) Iter[T]
}

type StepByIter

type StepByIter[T any] interface {
	Iter[T]
	StepBy(step int) Iter[T]
}

type TailIter

type TailIter[T any] interface {
	Iter[T]
	Tail() Iter[T]
}

type TakeIter

type TakeIter[T any] interface {
	Iter[T]
	Take(n int) Iter[T]
}

type TakeWhileIter

type TakeWhileIter[T any] interface {
	Iter[T]
	TakeWhile(take func(T) bool) Iter[T]
}

type ToStringByIter

type ToStringByIter[T any] interface {
	Iter[T]
	ToStringBy(sep string, f func(T) string) string
}

type ToStringIter

type ToStringIter[T fmt.Stringer] interface {
	Iter[T]
	ToString(sep string) string
}

type UnzipByIter

type UnzipByIter[A, B, C any] interface {
	Iter[A]
	UnzipBy(unzip func(A) (B, C)) (Iter[B], Iter[C])
}

type ZipByIter

type ZipByIter[A, B, C any] interface {
	Iter[A]
	ZipBy(other Iter[B], f func(A, B) C) Iter[C]
}

Jump to

Keyboard shortcuts

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