kex docs Standard Library

Enumerable

Foldable — traversal operations derived from a left fold.

Blocks are applied via Kex.Intrinsic.Fun.applyItem, which auto-splats a pair item into a two-argument block.

trait Foldable

Implemented by [X], Map<K, V>, Range, Set<A>, UnorderedSet<A>, String.

Required methods

An implementor writes these.

reduce

reduce : A -> (A -> T -> A) -> A

Provided methods

Every implementor gets these, written in terms of the required methods.

each

each(f)

Applies f to each item for its side effects. Returns unit.

eachIndexed

eachIndexed(f)

Applies f to each item together with its 0-based position, for side effects. Returns unit. The index is the LAST block parameter, so a Map entry can be taken either whole (|entry, i|) or spread (|k, v, i|).

Examples
["a", "b"].eachIndexed { |s, i| IO.printLine("${i}: ${s}") }

all?

all?(pred)

True if every item satisfies pred.

any?

any?(pred)

True if at least one item satisfies pred.

find

find(pred)

The first item satisfying pred wrapped in Just, or None.

count

count(pred)

Counts the items for which pred holds.

trait Enumerable

Enumerable — collection-producing operations derived from a left fold.

Implemented by [X], Map<K, V>, Range, Set<A>, UnorderedSet<A>, String.

Required methods

An implementor writes these.

reduce

reduce : A -> (A -> T -> A) -> A

Provided methods

Every implementor gets these, written in terms of the required methods.

map

map(f)

Transforms each item by applying f, collecting the results into a list.

mapIndexed

mapIndexed(f)

Transforms each item together with its 0-based position, collecting the results into a list. Index is the LAST block parameter — see eachIndexed.

Examples
["a", "b"].mapIndexed { |s, i| "${i}${s}" }   # => ["0a", "1b"]

filter

filter(pred)

Keeps the items for which pred holds.

flatMap

flatMap(f)

Maps each item to a list and concatenates the results.

collect

collect(f)

Maps each item through f (returning an Optional), keeping and unwrapping the Just(y) results and dropping None. Filter + map fused via an Optional-returning function.