Control structures

Chalk’s two control structures, decided as the document compiles: @for repeats source — a count, a range or each row of a dataset — and @if keeps or drops it.

On this page

Chalk has two control structures: @for, a loop, and @if, a conditional. Both are decided as the document compiles, and the internote a reader opens holds only what they produced.

#Repeating with @for

@for writes its body out again for every pass. Its implicit value says how many passes: a whole count, counted from 1, an inclusive range that counts upward, or a dataset, one pass per row.

@for:3{
    Attempt {{i}} of 3.
}

That is three paragraphs, Attempt 1 of 3. to Attempt 3 of 3. @for:0 writes nothing; @for:-2..2 makes five passes, −2 to 2. A range written downward is refused: descending range `3..1` — `from..to` iterates upward, so `from` must not exceed `to`.

The body is any Chalk that is legal where the @for stands. Inside a graph it repeats marks; at the top of the document it repeats whole scenes. The counter is spliced in as text before the body is read, so it lands wherever text can — in a heading, in maths, in an expression:

@for:2..4{
    scene{
        ## $x^{{i}}$

        Near the origin, $x^{{i}}$ is flatter than $x^{{i - 1}}$.
    }[
        graph{
            curve(expression: x^{{i}}; domain: [-1.5, 1.5])
        }(
            y domain: [-2, 2]
        )
    ]
}

Three scenes, for x^2, x^3 and x^4.

Inside one graph, the same loop draws a family of curves, each pass a curve with its own label:

Four curves from the origin through (1, 1) — x, x², x³ and x⁴ — each labelled with its formula, the higher powers flatter below x = 1 and steeper above it.Four curves from the origin through (1, 1) — x, x², x³ and x⁴ — each labelled with its formula, the higher powers flatter below x = 1 and steeper above it.
graph{
    @for:1..4{
        curve(expression: x^{{i}}; domain: [0, 1.3]; label: $x^{{i}}$)
    }
}(
    x-domain: [0, 1.4]
    y-domain: [0, 3]
)

#The counter and its ids

The counter is {{i}} unless as: names it. Inside the body it is a number, so {{i^2}} and {{i - 1}} are arithmetic.

Warning
Nested loops each need their own name, because an inner loop with the same name hides the outer one's counter.
@for(n: 1..3; as: row){
    @for(n: 1..3; as: col){
        {{row}} × {{col}} = {{row * col}}.
    }
}

The counter exists only inside the body. So does anything the body declares: a @let inside a pass is gone after it, and a @let outside with the counter's name is hidden inside the loop and back afterwards.

An id written in the body is written once per pass, and ids are unique across the document, so a fixed id is refused on the second pass:

graph{
    @for:0..2{
        point#p(x: {{i}}; y: 0)
    }
}

[reference]duplicate id `#p` — already declared by a node at line 3

Splicing the counter into the id gives one id per pass, and each can be reached like any other:

scene{
    The last point sits at height {{#p-4.y}}.
}[
    graph{
        @for:0..4{
            point#p-{{i}}(x: {{i}}; y: {{i^2}})
        }
    }(
        x domain: [-1, 5]
        y domain: [-1, 17]
    )
]

Inside a template, ids made this way are private to each use; Modularity covers how.

#Once per row of a dataset

Given a dataset's id, @for makes one pass per row, top to bottom. {{i}} is the row number from 1 and {{i.<column>}} that row's cell:

@data#islands{
    island     | area   | species
    Baltra     | 25.09  | 58
    Bartolomé  | 1.24   | 31
    Santa Cruz | 903.82 | 444
}

@for:#islands{
    {{i}}. {{i.island}} covers {{i.area}} km$^2$.
}

The @data comes first: `@for` reads its dataset in document order, so the `@data` must come first. A pack's dataset is walked the same way, @for:#biology.lynx-hare (Modularity). How cells are named, what a hyphenated column can and cannot do in arithmetic, and the refusals for a column or a boundary file in place of a dataset are on Datasets.

#Keeping or dropping with @if

@if keeps its body when its condition holds, and its detail, if it has one, when it fails. The condition is when:, an expression that gives true or false:

@let(audience: teacher)
@let(answers)

@if(when: audience = teacher){
    note{
        Answers are collected in the closing scene.
    }
}[
    note{
        Answer each question before moving on.
    }
]

@if(when: audience = teacher and answers){
    The worked answers follow each question.
}
  • <, <=, > and >= compare numbers, and dates by which comes first; and, or and not join conditions; x in 0..5 tests a range, either end of which may be left open (x in 5..) or left out of it with a < beside the dots (x in 0..<5, x in 0<..); true and false are themselves.
  • = and != compare loosely: Teacher = teacher and 1.0 = 1 hold. == and !== compare exactly, so Teacher == teacher does not. Two texts compare as text: under @let(a: 1.0) and @let(b: 1), a = b fails and a = 1 holds, as "1.0" = "1" fails.
  • Beside =, a word nothing declares is a name, kept as written and possibly several words: audience = teacher, country = new zealand. Quoted, it is text and nothing else: country = "new zealand".
  • A bare key tests for true and !key for false: @if(answers) is @if(when: answers). A bare @let(answers) is true.
  • Inside a condition a hyphen is a minus, so a hyphenated constant goes in braces: @if(when: {{show-value}} and answers).
  • A condition gives a boolean, never a number: @if: `k + 1` gives a number where a boolean is wanted — compare it, as in `k + 1 > 0`. With no detail, a failed @if leaves nothing behind.
  • The constant is declared above. Bare, a name nothing declares is the word itself: @if(when: level) is @if: `level` holds `level`, which is not a boolean, and @if(when: level > 3) is @if: `level` is not a valid number. A template's attributes and a @for counter or cell count as constants.

A key: value pair is not a condition, and is refused with the fix: @if: `audience: teacher` is not an attribute of `@if` — write `@if(when: audience = teacher)`. The same operators work in any {{…}} and in every attribute that takes a condition, on Expressions.

What the kept branch declares stays inside it, as in a @for body.

Warning
Both directives stand on a line of their own, in block position; written mid-sentence, @if(answers){is four} is printed as written.

#Choosing rows

A @for over a dataset has no filter or sort. An @if in its body chooses rows, by a cell or by the row number:

@data#islands{
    island     | area   | species | inhabited
    Baltra     | 25.09  | 58      | true
    Bartolomé  | 1.24   | 31      | false
    Santa Cruz | 903.82 | 444     | true
}

graph{
    @for(n: #islands; as: row){
        @if(row.inhabited){
            point(x: {{row.area}}; y: {{row.species}}; label: {{row.island}})
        }
    }
}

Two points, Baltra and Santa Cruz. @if(when: row.island = Santa Cruz) keeps one row by name, @if(when: row = 1) the first, and @if(when: row.area > 10 and row.inhabited) the rows that pass both tests.

#Decided as the document compiles

@for and @if run while the document compiles. The compiled internote holds the passes and the kept branch, and nothing a reader does changes which. So a count and a condition read only what is settled then: written values, @let constants, a template's attributes, loop counters and cells, and expressions over them.

@let(sides: 6)
@let(diagonals: {{sides * (sides - 3) / 2}})

@for(n: {{diagonals}}){
    Diagonal {{i}} of {{diagonals}}.
}

A computed count is written in the named form, @for(n: {{…}}); the implicit value is a single token, and @for:{{diagonals}} is refused with that fix. A value the reader can move — a fit's slope, a parameter — is bound, and refused. Here #trend is a fit:

@let(slope: {{#trend.slope}})

@if(when: slope > 0.9){
    The trend rises.
}

[expression]`@if` is decided while the document compiles and cannot depend on `#trend.slope` (`when: slope > 0.9` is bound)

What changes as the internote is read lives on the canvas: steps and cues on Motion, parameters set by controls on Controls. Which values are bound is on Expressions.

Last updated