foreach

.foreach iterable:{Iterable<Any>} body:{Lambda} -> Iterable<Any>

Repeats content for each element of an iterable collection. The current element can be accessed via the lambda argument, which may be either explicit or implicit.

.var {collection}
- A
- B
- C

.foreach {.collection}
element:
The current element is **.element**

In implicit form:

.foreach {.collection}
The current element is **.1**

In case the iterable is destructurable (e.g. a dictionary or pair) and the lambda body has more than 1 explicit parameter, the value is destructured into components.

.var {x}
.dictionary
- a: 1
- b: 2
- c: 3

.foreach {.x}
key value:
**.key** has value **.value**

The output is a collection containing the output of each iteration (mapping), so, if used as a value, this function has a meaning similar to map in many languages.

.var {collection}
- A
- B
- C

.var {mappedcollection}
.foreach {.collection}
item:
.item::lowercase

.mappedcollection::first

Output: a

Note that, like any lambda, its content can be inlined by means of the @lambda annotation. The previous snippet can be simplified as follows:

.foreach {.collection} {@lambda item: .item::lowercase}::first

Return

a collection that contains the output of each iteration

Parameters

iterable

collection to iterate

body

the output of each iteration. Accepts 1 parameter (the current element).

Wiki page

Loops