Skip to content

#[mixable] Iter[T]

Abstract type for iterators, whose instances have the methods below. Built-in iterators are Iter; a class that only implements (next) is not.

Iter is not constructible directly. See Classes for defining custom iterators.

assert_eq (type $ [1, 2, 3].iter()) $Iter

Inherits from: Iterable[T]

Implements: BaseIter[T], Spread[T]

Methods

(iter)() -> Iter[T]

Returns this iterator.

(next)() -> T

Returns the next value, raising IterStop once exhausted.

(spread)() -> Spread[T]

Spreads the remaining items positionally.

all … -> Bool

Consumes the iterator and tests whether every value is truthy, or satisfies pred, as Iterable.all does.

Parameters

NameTypeDescription
pred? ((T) -> Value)

any … -> Bool

Consumes the iterator and tests whether any value is truthy, or satisfies pred, as Iterable.any does.

Parameters

NameTypeDescription
pred? ((T) -> Value)

chain[*Us] *values -> Iter[Union[T, ...Us]]

Returns an iterator that yields this iterator's values followed by those of each additional iterable.

Parameters

NameTypeDescription
*values ...Iterable[Us]

chomp() -> Iter[Str] overload 1 of 2

Returns an iterator with one trailing line terminator removed from each value, as Iterable.chomp does.

Parameters

NameTypeDescription
self Iter[Str] Receiver.

chomp() -> Iter[Bin] overload 2 of 2

Returns an iterator with one trailing line terminator removed from each value, as Iterable.chomp does.

Parameters

NameTypeDescription
self Iter[Bin] Receiver.

count() -> Int

Consumes the iterator and returns the number of values it yielded.

crimp … -> Iter[Str] overload 1 of 2

Returns an iterator with a line terminator appended to each value, as Iterable.crimp does.

Parameters

NameTypeDescription
self Iter[Str] Receiver.
terminator? (Str | Bin)

crimp … -> Iter[Bin] overload 2 of 2

Returns an iterator with a line terminator appended to each value, as Iterable.crimp does.

Parameters

NameTypeDescription
self Iter[Bin] Receiver.
terminator? (Str | Bin)

enumerate() -> Iter[Tuple[Int, T]]

Returns an iterator of (index, value) tuples, counting from 0.

filter pred -> Iter[T]

Returns an iterator of the values for which pred is truthy.

Parameters

NameTypeDescription
pred ((T, <Value, >Value) -> Value)

find[D = T] pred … -> (T | D)

Consumes values up to and including the first for which pred is truthy, and returns it, as Iterable.find does.

Parameters

NameTypeDescription
pred ((T) -> Value)
:default? D
:else? (() -> D)

fold[A] init func -> A

Consumes the iterator, accumulating a value as Iterable.fold does.

Parameters

NameTypeDescription
init A
func ((A, T) -> A)

iter() -> Iter[T]

Returns this iterator.

kv[K, V]() -> Spread[K, V]

Adapts the remaining elements for keyed spreading.

Each element must unpack into exactly two values without being consumed. Spreading consumes the source. The adapter does not support iteration.

let entries = [("x", 1), ("y", 2)].iter()
assert_eq {...entries.kv()} {"x": 1, "y": 2}

Parameters

NameTypeDescription
self Iter[Unpack[{K, V}]] Receiver.

map[U] func -> Iter[U]

Returns an iterator that maps each value through func.

Parameters

NameTypeDescription
func ((T, <Value, >Value) -> U)

max[D = T] … -> (T | D)

Consumes the iterator and returns the largest value, as Iterable.max does.

Parameters

NameTypeDescription
:default? D

min[D = T] … -> (T | D)

Consumes the iterator and returns the smallest value, as Iterable.min does.

Parameters

NameTypeDescription
:default? D

next[D = T] … -> (T | D)

Returns the next value from the iterator.

Parameters

NameTypeDescription
:default? D
:else? (() -> D)

Errors

Raises IterStop when exhausted and there is no default or else.

skip n -> Iter[T]

Returns an iterator that discards the first n values, then yields the rest, as Iterable.skip does.

Parameters

NameTypeDescription
n Int

take n -> Iter[T]

Returns an iterator that yields at most n values, as Iterable.take does.

Parameters

NameTypeDescription
n Int

zip[*Us] *values -> Iter[Tuple[T, ...Us]]

Returns an iterator of tuples across this iterator and the additional iterables, which stops at the end of the shortest.

Parameters

NameTypeDescription
*values ...Iterable[Us]