Nanyx

iter

Generic iterable helpers via Iter(a) context

The iter module provides generic operations for any type a for which an Iter(a) context is provided.

Contexts

Iter(a)

context Iter(a) = (
  elem: type
  next: a -> #some(elem, a) | #done
)

The element type for the iterable is stored as an associated type on the Iter(..) context with the name elem; for an iterable argument, it is always available as elem in the function signature (or Iter(a).elem, if disambiguation is needed).

Definitions

all

all: [Iter(a)] (a, (elem -> bool)) -> bool

Returns true if all elements satisfy the predicate.

items\all { > 0 }

any

any: [Iter(a)] (a, (elem -> bool)) -> bool

Returns true if any element satisfies the predicate.

items\any { > 0 }

count

count: [Iter(a)] a -> int

Counts elements by iterating through the input.

items\count

find

find: [Iter(a)] (a, (elem -> bool)) -> #some(elem) | #notFound

Finds the first matching element.

items\find { .id == targetId }

first

first: [Iter(a)] a -> #some(elem) | #emptyIterable

Returns the first element wrapped in #some, or #emptyIterable.

items\first

firstOr

firstOr: [Iter(a)] (a, elem) -> elem

Returns the first element, or the fallback when the iterable is empty.

items\firstOr(fallback)

fold

fold: [Iter(a)] (a, s, ((s, elem) -> s)) -> s

Folds elements from left to right.

items\fold(0) { + }

toList

toList: [Iter(a)] a -> list(elem)

Materializes an iterable into a list.

items\toList