Landin library reference discuss source

Shared: available on every enabled target.

The concept a for loop walks a value through.

for x in s over a struct or an any C uses s's conformance to iter.iterable. A concept named iterable declared anywhere else is not used. A conformance names a cursor type and an item type and supplies first, at_end, item and next, each given a read-only pointer to the loop's own copy of the source; item hands out a value of its own, never a view of the source. core/map and core/tree conform.

Items

Concepts 1

Also on this page

iterable

concept core/iter/iter.ldn:14 ↑ index
public iterable: type = concept (t: type, cur: type, item_type: type)

The cursor of the first item, or one at_end already.

    first: (s: ptr t) -> (c: cur)

Whether c is past the last item.

    at_end: (s: ptr t, c: cur) -> (yes: bool)

The item at c, a value of its own.

    item: (s: ptr t, c: cur) -> (v: item_type)

The cursor after c.

    next: (s: ptr t, c: cur) -> (c2: cur)
end iterable

What for x in s walks. A conformance supplies the cursor type cur and the type of the items handed out, item_type, and the four entries: first gives the cursor of the first item, at_end says whether a cursor is past the last, item hands out the item at a cursor as a fresh value, never a view of the source, and next gives the cursor after it. Each entry gets a read-only pointer to the loop's own copy of the source. The signatures are exact: no permission, error set or from clause is inferred or converted. A container whose items are one slice hands out the slice instead, which for walks directly.

Example program

This complete program is one of the compiler's runtime tests. View source.

The program, 66 lines
compiler/tests/fixtures/runtime/for-over-map-and-tree/main.ldn66 lines
--  D1957: core/map and core/tree conform to core/iter's `iterable`, so
--  `for` walks them.  The map's walk skips a removed entry and keeps
--  insertion order; the tree's hands out every node's identifier, the
--  branch after its children.
import core/arena
import core/cmp
import core/map
import core/tree

walk_map: () -> (ok: bool) =
    ok = false
    mut backing: [2048]u8 = zeroed
    mut space: arena.space = zeroed
    memory := arena.over_unchecked(space, addr backing[0], lenof backing)
    mut value := map.new(key: u32, item: i32, seed: cmp.seed_from(1, 2))
    mut count: usize = 0
    for each in value do
        _ = each
        inc count
    end for
    return when count <> 0
    map.insert(value, memory, u32(30), 3) else none
    map.insert(value, memory, u32(10), 1) else none
    map.insert(value, memory, u32(20), 2) else none
    map.insert(value, memory, u32(40), 4) else none
    map.remove(value, u32(20)) else none
    --  Insertion order is 30, 10, 40: the keys read as one number, 301040.
    mut keys: u32 = 0
    mut sum: i32 = 0
    for each in value do
        keys = keys * 100 + each.key
        sum += each.item
        inc count
    end for
    ok = count == 3 and keys == 301040 and sum == 8
end walk_map

walk_tree: () -> (ok: bool) =
    ok = false
    mut backing: [1024]u8 = zeroed
    mut space: arena.space = zeroed
    memory := arena.over_unchecked(space, addr backing[0], lenof backing)
    mut value := tree.new()
    first := tree.add_leaf(value, memory, "a") else begin return end
    _ = tree.add_leaf(value, memory, "b") else begin return end
    top := tree.add_branch(value, memory, "top", first, 2) else begin return end
    mut seen: usize = 0
    mut leaves: usize = 0
    mut last: usize = 0
    for id in value do
        node := tree.get(value, id) else begin return end
        if tree.kind(node) == tree.leaf_node then
            inc leaves
        end if
        last = tree.ordinal(id)
        inc seen
    end for
    ok = seen == 3 and leaves == 2 and last == tree.ordinal(top)
end walk_tree

public main: () -> (code: i32) =
    code = 1
    if walk_map() and walk_tree() then
        code = 42
    end if
end main