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.
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}") }
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.
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"]