For the complete documentation index, see llms.txt. This page is also available as Markdown.
The main type of loop is provided by the .foreach function, which accepts:
Iterable value.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 as the input, containing the evaluation of the lambda for each iterated value. This means the function can be used as an expression, similarly to the map function in many programming languages.
Keep in mind that .1 implicitly refers to the first parameter of the lambda.
.row alignment:{spacearound}
.foreach {1..3}
.11
2
3
Any iterable value is accepted, including Markdown lists. See Iterable for all possible ways of defining an iterable value.
.var {letters}
- A
- B
- C
.foreach {.letters}
###! .1
The letter is **.1**.The letter is A.
The letter is B.
The letter is C.
The type of iterated elements is preserved. See Typing for more information.
.row alignment:{spacearound}
.foreach {1..5}
n:
.multiply {.n} by:{.n}1
4
9
16
25
To keep track of the position of each element, loop on the result of .withindices. It pairs each element with its index, starting at 1, as a dictionary, which can be destructured into the index and the element.
.var {letters}
- A
- B
- C
.letters::withindices::foreach
index letter:
.index .letter1 A
2 B
3 C
The .repeat {times} function is a shorthand for .foreach {1..times}.
.repeat {3}
.11
2
3
The .join function merges the elements of an iterable into a single inline content, separated by , or by a custom separator.
.var {letters}
- A
- B
- C
.letters::joinA, B, C
Like .foreach, it accepts a lambda block that transforms each element before joining.
.var {letters}
- A
- B
- C
.letters::withindices::join
index letter:
.index::text script:{sup} **.letter**1 A, 2 B, 3 C
The .findfirst function loops over an iterable until an element satisfies a condition, and returns that element. Like .foreach, it accepts an Iterable value and a single-parameter lambda block, which must return a boolean.
If no element satisfies the condition, the result is none.
.var {numbers}
- 3
- 8
- 12
.numbers::findfirst {n: .n::isgreater than:{5}}8
Here, we look up an element’s index within a list.
.var {letters}
- A
- B
- C
.letters::withindices::findfirst {index letter: .letter::equals {B}}::first2
The .findall function works the same way, but returns a collection of all the elements that satisfy the condition.
.var {numbers}
- 3
- 8
- 12
.numbers::findall {n: .n::isgreater than:{5}}::join8, 12