8.15.0.10
2.5 Repetitions🔗ℹ
As we saw in Lists, the list form supports repetition binding
via ..., and it can use a repetition via ...
when creating a new list:
In either bindings or expressions, ... can be nested to create
and use repetitions of greater depth. For example, using
... within a term that is already before
... creates a binding of depth 2, which is a repetition
of repetitions.
|
|
[[1, 2, 3, 100], [5, 6, 100]] |
In this example, both ...s are recognized directly by
the list binding form, but the list-construction expression follows a
more general rule. The outer ... (i.e., the last one)
allows any repetition of depth 1 before it, and [z, ..., 100]
creates a repetition of depth 1 by using ... after
z, which is a repetition of depth 2.
When a repetition is followed by multiple ...s in a row, as
opposed to nested ...s, then the repetitions that would be
accessed by nesting are flattened into a single repetition. This
flattening has the effect of appending sequences.
Some other expression-like forms serve as repetition forms when they are
used in a repetition position. For example, an operator that is defined
by operator forms a repetition when it has repetition
arguments, so a negation term -x creates a repetition of depth
1 when x is a repetition of depth 1:
When a repetition is built from multiple other repetitions, the
repetitions are used in parallel. For example, + used on two
repetitions maps addition over the repetitions. The two repetitions must
have the same length.
When repetitions of different depths are combined, the shallower
repetition is repeated for outer layers of the deeper repetition.
|
|
|
[["a1", "b2"], ["a3", "b4"], ["a5", "b6"]] |
A literal value or a variable works as a repetition of depth 0. A
repetition of depth 0 is not useful in itself, but it’s useful in
combination with a repetition of greater depth. For example, using
1 as a repetition lets us add it to every element of a
repetition.
|
|
[2, 3, 4] |
|
|
[6, 7, 8] |
|
five: used with wrong repetition depth |
expected: 0 |
actual: 1 |
Analogous to lists, map and set constructions work as repetition forms.
|
|
[{1}, {2}, {3}] |
|
[{1: #true}, {2: #true}, {3: #true}] |
Function calls, the . operator, array or map access via
[], and syntax templates all work as repetition forms, too,
given other repetitions to start with.
|
> fun mirror(Posn(x, y)) :~ List.of(Posn): | [Posn(x, y), Posn(y, x)] |
|
> def [p, ...] = [Posn(1, 2), Posn(3, 4), Posn(5, 6)] |
|
> ['z + $(mirror(p)[1].x)', ...] |
|
['z + 2', 'z + 4', 'z + 6'] |