foreach
.foreach iterable:{Iterable<Any>} body:{Lambda} -> Iterable<Any>Content copied to clipboard
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**Content copied to clipboard
In implicit form:
.foreach {.collection}
The current element is **.1**Content copied to clipboard
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**Content copied to clipboard
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::firstContent copied to clipboard
Output:
a
Its content can also be inlined. The previous snippet can be simplified as follows:
.foreach {.collection} {item: .item::lowercase}::firstContent copied to clipboard
Return
a collection that contains the output of each iteration
Parameters
iterable
collection to iterate
body
- Likely a body argument
the output of each iteration. Accepts 1 parameter (the current element).