Skip to content
Giorgio Garofalo edited this page Apr 27, 2025 · 6 revisions

For-each

The main type of loop is provided by the .foreach function, which accepts:

  1. An Iterable value;
  2. A 1-parameter lambda block, where the argument is the current item being iterated.
.foreach {2..4}
  n:
  The number is: **.n**

The number is: 2

The number is: 3

The number is: 4

 

The function returns an ordered iterable collection of the same size of the input one, containing the evaluation of the lambda for each iterated value.
Thus, the function can be used as an expression:

.row alignment:{spacearound}
  .foreach {1..3}
    *.1* <!-- .1 is an implicit lambda argument: refers to the first parameter -->
Result 1

 

Any iterable value is accepted. Here we use a Markdown list:

.var {letters}
  - A
  - B
  - C

.foreach {.letters}
  ### .1

  The letter is **.1**.
Result 2

See Iterable for all possible ways of defining an iterable value.

 

The type of iterated elements is preserved (see Typing for more):

.row alignment:{spacearound}
  .foreach {1..5}
    n:
    .multiply {.n} by:{.n}
Result 3

 

Repeat

The .repeat {times} function is a shorthand for .foreach {1..times}.

.repeat {3}
  .1

1

2

3

Clone this wiki locally