Skip to content

#[mixable] Iterable[T]

Abstract type for iterable values, whose instances have the methods below. Arrays, dicts, and ranges are Iterable; a Record, or a class that only implements (iter), is not.

Implements: BaseIterable[T]

Methods

(iter)() -> BaseIter[T]

Returns an iterator over the items.

all … -> Bool

Tests whether every item is truthy, or satisfies pred. An empty iterable passes.

Parameters

NameTypeDescription
pred? ((T) -> Value)

any … -> Bool

Tests whether any item is truthy, or satisfies pred. An empty iterable fails.

Parameters

NameTypeDescription
pred? ((T) -> Value)

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

Returns an iterator over the items, followed by the items of each additional iterable.

Parameters

NameTypeDescription
*values ...Iterable[Us]

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

Returns an iterator with one trailing line terminator (\r\n or \n) removed from each item, if present.

Distinct from Str.trim_end, which is about whitespace generally and takes an optional character set. chomp is about a line terminator specifically and takes nothing.

Parameters

NameTypeDescription
self Iterable[Str] Receiver.

Errors

Getting an item raises TypeError if it is neither a Str nor a Bin.

Example

assert_eq [...["a\n", "b\r\n", "c"].chomp()] ["a", "b", "c"]

for line = shell.stdin.chomp()
  echo "got $line"

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

Returns an iterator with one trailing line terminator (\r\n or \n) removed from each item, if present.

Distinct from Str.trim_end, which is about whitespace generally and takes an optional character set. chomp is about a line terminator specifically and takes nothing.

Parameters

NameTypeDescription
self Iterable[Bin] Receiver.

Errors

Getting an item raises TypeError if it is neither a Str nor a Bin.

Example

assert_eq [...["a\n", "b\r\n", "c"].chomp()] ["a", "b", "c"]

for line = shell.stdin.chomp()
  echo "got $line"

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

Returns an iterator with a line terminator appended to each item.

The inverse of chomp, and the usual way to terminate values on their way into a byte stream. The terminator is appended unconditionally: an item that already ends in one gets a second.

Parameters

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

Errors

Raises TypeError for an item, or a terminator, that is neither a Str nor a Bin, or if a Bin terminator would leave a Str item invalid UTF-8.

Example

assert_eq [...["a", "b"].crimp()] ["a\n", "b\n"]
assert_eq [...["a"].crimp("\r\n")] ["a\r\n"]

run cmd stdin: (["one", "two"].crimp())

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

Returns an iterator with a line terminator appended to each item.

The inverse of chomp, and the usual way to terminate values on their way into a byte stream. The terminator is appended unconditionally: an item that already ends in one gets a second.

Parameters

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

Errors

Raises TypeError for an item, or a terminator, that is neither a Str nor a Bin, or if a Bin terminator would leave a Str item invalid UTF-8.

Example

assert_eq [...["a", "b"].crimp()] ["a\n", "b\n"]
assert_eq [...["a"].crimp("\r\n")] ["a\r\n"]

run cmd stdin: (["one", "two"].crimp())

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

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

filter pred -> Iter[T]

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

Parameters

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

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

Returns the first item for which pred is truthy.

Parameters

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

Errors

Raises RuntimeError if no item matches and there is no default or else.

fold[A] init func -> A

Accumulates a value left to right, calling func with the accumulated value and each item. Returns init if there are no items.

Parameters

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

iter() -> Iter[T]

Returns an iterator over the value.

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

Adapts the elements for keyed spreading, using iter.

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

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

Parameters

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

map[U] func -> Iter[U]

Returns an iterator that maps each item through func.

Parameters

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

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

Returns the largest item.

Parameters

NameTypeDescription
:default? D

Errors

Raises IterStop if there are no items and no default.

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

Returns the smallest item.

Parameters

NameTypeDescription
:default? D

Errors

Raises IterStop if there are no items and no default.

skip n -> Iter[T]

Returns an iterator that discards the first n items.

Parameters

NameTypeDescription
n Int

Errors

Raises ValueError if n is negative.

take n -> Iter[T]

Returns an iterator yielding at most n items.

Parameters

NameTypeDescription
n Int

Errors

Raises ValueError if n is negative.

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

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

Parameters

NameTypeDescription
*values ...Iterable[Us]