Documentation
¶
Index ¶
- func AdvanceBy[T any](iter Iterator[T], n uint64) result.Result[struct{}, uint64]
- func All[T any](iter Iterator[T], pred func(T) bool) bool
- func Any[T any](iter Iterator[T], pred func(T) bool) bool
- func Chain[T any](first Iterator[T], second Iterator[T]) *chainIterator[T]
- func Collect[T any](iter Iterator[T]) []T
- func CollectInto[T any, C Collection[T]](iter Iterator[T], collection C) C
- func Count[T any](iter Iterator[T]) uint64
- func Enumerate[T any](iter Iterator[T]) *enumerateIterator[T]
- func Filter[T any](iter Iterator[T], pred func(T) bool) *filterIterator[T]
- func FilterMap[T any, U any](iter Iterator[T], pred func(T) option.Option[U]) *filterMapIterator[T, U]
- func Find[T any](iter Iterator[T], pred func(T) bool) option.Option[T]
- func Fold[T any, A any](iter Iterator[T], a A, f func(A, T) A) A
- func ForEach[T any](iter Iterator[T], f func(T))
- func FromSeq[T any](seq iter.Seq[T]) *seqIterator[T]
- func FromSeq2[K any, V any](seq iter.Seq2[K, V]) *seq2Iterator[K, V]
- func Fuse[T any](inner Iterator[T]) *fuseIterator[T]
- func Inspect[T any](inner Iterator[T], f func(T)) *inspectIterator[T]
- func Intersperse[T any](inner Iterator[T], item T) *intersperseIterator[T]
- func IntersperseWith[T any](inner Iterator[T], f func() T) *intersperseWithIterator[T]
- func Last[T any](iter Iterator[T]) option.Option[T]
- func Map[T any, U any](iter Iterator[T], f func(T) U) *mapIterator[T, U]
- func MapWhile[T any, U any](inner Iterator[T], pred func(T) option.Option[U]) *mapWhileIterator[T, U]
- func Max[T cmp.Ordered](iter Iterator[T]) option.Option[T]
- func MaxBy[T any](iter Iterator[T], less func(T, T) bool) option.Option[T]
- func Min[T cmp.Ordered](iter Iterator[T]) option.Option[T]
- func MinBy[T any](iter Iterator[T], less func(T, T) bool) option.Option[T]
- func Nth[T any](iter Iterator[T], n uint64) option.Option[T]
- func Partition[T any](iter Iterator[T], f func(T) bool) ([]T, []T)
- func Position[T any](iter Iterator[T], pred func(T) bool) option.Option[uint64]
- func Range[T numeric](start T, end T) *rangeIterator[T]
- func RangeBy[T numeric](start T, end T, step T) *rangeIterator[T]
- func RangeInclusive[T numeric](start T, end T) *rangeIterator[T]
- func RangeInclusiveBy[T numeric](start T, end T, step T) *rangeIterator[T]
- func Scan[T any, S any, U any](inner Iterator[T], initial S, f func(*S, T) option.Option[U]) *scanIterator[T, S, U]
- func Skip[T any](inner Iterator[T], n uint64) *skipIterator[T]
- func SkipWhile[T any](inner Iterator[T], f func(T) bool) *skipWhileIterator[T]
- func StepBy[T any](inner Iterator[T], n uint64) *stepByIterator[T]
- func Take[T any](inner Iterator[T], n uint64) *takeIterator[T]
- func TakeWhile[T any](inner Iterator[T], f func(T) bool) *takeWhileIterator[T]
- func ToSeq[T any](it Iterator[T]) iter.Seq[T]
- func ToSeq2[K any, V any](it Iterator[pair.Pair[K, V]]) iter.Seq2[K, V]
- func TryCollect[T any, E any](iter Iterator[result.Result[T, E]]) result.Result[[]T, E]
- func TryFold[T any, A any, E any](iter Iterator[T], a A, f func(A, T) result.Result[A, E]) result.Result[A, E]
- func Unzip[T any, U any](iter Iterator[pair.Pair[T, U]]) ([]T, []U)
- func Zip[T any, U any](first Iterator[T], second Iterator[U]) *zipIterator[T, U]
- type CloseableIterator
- type Closer
- type Collection
- type IntoIterator
- type Iterator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdvanceBy ¶
Advances the iterator by n elements. This method will eagerly skip n elements by calling next up to n times until Nothing is encountered. Returns Ok[struct{}{}] if successful. Returns Err[k] if Nothing is encountered, where k is the number of elements advanced before hitting the end.
func All ¶
Tests if every element of the iterator matches the predicate. Applies pred to each element of the iterator, until the iterator returns Nothing. Once the first element returns false, all() will short-circuit and exit. The empty iterator returns true.
func Any ¶
Tests if any element of the iterator matches the predicate. Applies pred to each element of the iterator, until the iterator returns Nothing. Once the first element returns true, any() will short-circuit and exit. The empty iterator returns false.
func Chain ¶
Returns a new iterator that delivers all elements from first, then second once first is exhausted.
func CollectInto ¶
func CollectInto[T any, C Collection[T]](iter Iterator[T], collection C) C
CollectInto collects the iterator into the given collection. The provided collection is modified to hold the elements in iter.
func Count ¶
Consumes the iterator, counting the number of elements until it was exhausted. Next() will be called at least once even if the iterator has no elements.
func Enumerate ¶
Returns a new iterator that delivers Pairs of indexes and values from the wrapped iterator.
func FilterMap ¶
func FilterMap[T any, U any](iter Iterator[T], pred func(T) option.Option[U]) *filterMapIterator[T, U]
Creates an iterator that both filters and maps. The returned iterator yields only the values for which the supplied closure returns Some(value).
func Find ¶
Find searches for the first element of the iterator that satisfies the predicate. Returns Some[T] for the first element that returns true. Short-circuits upon finding the first true element. If no element satisfies the predicate, returns Nothing.
func Fold ¶
Folds every element into an accumulator by applying an operation, returning the final either. The entire iterator will be consumed by this.
func FromSeq ¶
FromSeq converts an iter.Seq[T] to a seqIterator[T]. The sequence will be pulled from lazily as Next() is called.
The returned iterator holds resources from iter.Pull. It is intended that Close be called via defer immediately after creating the iterator. This ensures resources are released even if the iterator is not fully consumed. Close is safe to call multiple times or after the iterator is exhausted.
Important: You should defer Close even if the iterator is chained into other adapters like Map, Filter, Take, etc. The adapters do not propagate Close to the underlying iterator, so the original CloseableIterator must be closed explicitly.
Example:
it := iterator.FromSeq(slices.Values(mySlice))
defer it.Close()
// Safe to chain into adapters after deferring Close
mapped := iterator.Map(it, func(x int) int { return x * 2 })
result := iterator.Collect(mapped)
func FromSeq2 ¶
FromSeq2 converts an iter.Seq2[K, V] to a seq2Iterator[K, V]. The sequence will be pulled from lazily as Next() is called.
The returned iterator holds resources from iter.Pull2. It is intended that Close be called via defer immediately after creating the iterator. This ensures resources are released even if the iterator is not fully consumed. Close is safe to call multiple times or after the iterator is exhausted.
Important: You should defer Close even if the iterator is chained into other adapters like Map, Filter, Take, etc. The adapters do not propagate Close to the underlying iterator, so the original CloseableIterator must be closed explicitly.
Example:
it := iterator.FromSeq2(maps.All(myMap))
defer it.Close()
// Safe to chain into adapters after deferring Close
filtered := iterator.Filter(it, func(p pair.Pair[K, V]) bool { return p.First != "" })
result := iterator.Collect(filtered)
func Intersperse ¶
Creates a new iterator which places a copy of separator between adjacent items of the original iterator. A copy of item will be placed before the final Nothing. The item will only be shallow-copied. If you desire deep copying/bespoke behavior, use IntersperseWith.
func IntersperseWith ¶
Creates a new iterator which calls f and places a copy of its return value between items of the original iterator. Before the final Nothing, f will be called and its result will be returned.
func Last ¶
Last returns the final element of the iterator, before it returns Nothing. Returns Nothing if the iterator is empty.
func Map ¶
Creates an iterator that maps elements from the original iterator. Converts an iterator from type T to U.
func MapWhile ¶
func MapWhile[T any, U any](inner Iterator[T], pred func(T) option.Option[U]) *mapWhileIterator[T, U]
Returns an iterator yielding elements from inner, mapped by pred. The iterator stops once pred returns Nothing. Yields elements while pred continues to return Some.
func Max ¶
Max returns the maximum element of the iterator. Returns the last element if multiple elements are equally maximal. Returns Nothing if the iterator is empty.
func MaxBy ¶
MaxBy returns the maximum element of the iterator with respect to the specified less function. less(a, b) should return true if a is less than b, and false otherwise. Returns the last element if multiple elements are equally maximal. Returns Nothing if the iterator is empty.
func Min ¶
Min returns the minimum element of the iterator. Returns the first element if multiple elements are equally minimal. Returns Nothing if the iterator is empty.
func MinBy ¶
MinBy returns the minimum element of the iterator with respect to the specified less function. less(a, b) should return true if a is less than b, and false otherwise. Returns the first element if multiple elements are equally minimal. Returns Nothing if the iterator is empty.
func Nth ¶
Advances the iterator by n and returns the nth next item. Count starts from 0, so Nth(I, 0) returns the current element. The iterator is not rewinded, so preceding elements will be discarded. Subsequent calls (even to Nth(I, 0)) will return different values. Returns Nothing if n is greater or equal to the length of the iterator.
func Partition ¶
Consumes an iterator, producing two lists from it. The first contains all the elements the predicate returned true for, and the second, false.
func Position ¶
Searches for an element in an iterator, returning its index. Returns Nothing if it was not found. Consumes the iterator up to the item that returned true.
func Range ¶
func Range[T numeric](start T, end T) *rangeIterator[T]
Returns an iterator ranging from start (inclusive) to end (exclusive), stepping by 1. If end is less than start, the iterator will be empty.
func RangeBy ¶
func RangeBy[T numeric](start T, end T, step T) *rangeIterator[T]
Returns an iterator ranging from start (inclusive) to end (exclusive), stepping by step. If end is less than start, the iterator will be empty. This can be used with a negative step. If so, if end is greater than start, the iterator will be empty. NOTE: A zero step will return start ad infinitum.
func RangeInclusive ¶
func RangeInclusive[T numeric](start T, end T) *rangeIterator[T]
Returns an iterator ranging from start (inclusive) to end (inclusive), stepping by 1. If end is less than start, the iterator will be empty.
func RangeInclusiveBy ¶
func RangeInclusiveBy[T numeric](start T, end T, step T) *rangeIterator[T]
Returns an iterator ranging from start (inclusive) to end (exclusive), stepping by step. If end is less than start, the iterator will be empty. This can be used with a negative step. If so, if end is greater than start, the iterator will be empty. NOTE: A zero step will return start ad infinitum.
func Scan ¶
func Scan[T any, S any, U any](inner Iterator[T], initial S, f func(*S, T) option.Option[U]) *scanIterator[T, S, U]
An iterator adapter similar to fold that holds internal state and produces a new iterator. Calls f with the current state and the current item, and yields a new item mapped by the closure. NOTE: The closure is responsible for updating the state; it is not automatically assigned the result of the closure.
func Skip ¶
Creates an iterator that skips the first n elements. If the iterator is too short, an empty iterator is returned.
func SkipWhile ¶
Creates an iterator that skips the elements for which pred is true. As soon as pred returns false, the predicate is no longer evaluated. Even if further elements would pass the predicate, they will still be yielded.
func StepBy ¶
Creates an iterator starting with the same element, but stepping by n each time. Will always return the first element of the iterator, regardless of the step provided.
func Take ¶
Creates an iterator yielding the first n elements, or fewer if the provided iterator ends first.
func TakeWhile ¶
Creates an iterator that takes the elements for which pred is true. As soon as pred returns false, the predicate is no longer evaluated. The rest of the elements will be ignored. The element for which false is returned will be removed from the iterator in order to check it.
func ToSeq ¶
ToSeq converts an Iterator[T] to an iter.Seq[T]. The returned sequence can be used in a for-range loop. The iterator will be consumed as the sequence is iterated.
func ToSeq2 ¶
ToSeq2 converts an Iterator[pair.Pair[K, V]] to an iter.Seq2[K, V]. This is useful for iterators that yield key-value pairs, such as those created by Zip or Enumerate.
func TryCollect ¶
TryCollect attempts to collect an iterator of Result[T, E] into []T. It short-circuits upon reaching the first Err variant, instead returning Err[E].
func TryFold ¶
func TryFold[T any, A any, E any]( iter Iterator[T], a A, f func(A, T) result.Result[A, E], ) result.Result[A, E]
Tries to fold every element into an accumulator by applying an operation, returning the final either. Short-circuits if the function returns Err, returning the Result.
func Unzip ¶
Consumes an entire iterator of pairs, producing two collections, for the first and second elements respectively.
func Zip ¶
‘Zips up’ two iterators into a single iterator of pairs. zip() returns a new iterator that will iterate over two other iterators. The iterator returns a tuple where the first element comes from the first iterator, and the second element comes from the second iterator. If either iterator returns None, next from the zipped iterator will return None.
Types ¶
type CloseableIterator ¶
CloseableIterator is an iterator that holds resources requiring cleanup. When the iterator is not consumed to exhaustion, Close must be called to release the underlying resources. It is safe to call Close multiple times or after the iterator is exhausted.
After Close is called, the iterator will return Nothing for all subsequent calls to Next. Any adapters (such as Map, Filter, Take, etc.) that wrap this iterator will also return Nothing, since they delegate to the underlying iterator's Next method.
type Closer ¶
type Closer interface {
Close()
}
Closer is an interface for iterators that hold resources and need explicit cleanup. Close should be called when the iterator is no longer needed and has not been fully consumed. Calling Close on an already-closed or exhausted iterator is safe.
type Collection ¶
type Collection[T any] interface { // Inserts the provided elements into the collection. Append(...T) }
Collection represents a collection of elements of type T.
type IntoIterator ¶
IntoIterator is an interface representing something that can turn into an Iterator.
type Iterator ¶
Iterator returns items via successive Next calls until it has run out. It signals that the iterator is now empty by returning Nothing. NOTE: After an iterator returns Nothing for the first time, there is no guarantee that successive calls to NOTE: Iterator.Next() will continue to return Nothing. If you require this, use Fuse().