← All examples

Lockers

lockers.zg
pub const 'capacity' := 12;

struct Locker {
    immut number: int32,
    owner: str.utf8,
    items: list[str.utf8]
}

fn free_slots [lent 'l': Locker] -> int32 {
    return 'capacity' minus list.length[lend 'l'.items];
}

fn stash [lent.mut 'l': Locker, 'item': str.utf8] -> bool {
    if free_slots[lend 'l'] equal_to 0 { return false; }
    list.append[lend.mut 'l'.items, value give 'item'];
    return true;
}

fn label [lent 'l': Locker] -> str.utf8 {
    return "#" ++ to.stored.str.utf8['l'.number] ++ " (" ++ 'l'.owner ++ ")";
}

MAIN {
    let 'mine' := Locker[number = 7, owner = "Tankun", items = list.empty];
    let 'names' := ["helmet", "gloves", "rope"];
    let mut 'stored': int32 = 0;

    for 'n' in lend 'names' {
        if stash[lend.mut 'mine', copy 'n'] { change 'stored' = 'stored' plus 1; }
    }
    std::print.useful[label[lend 'mine'], ": ", 'stored', " stored, ", free_slots[lend 'mine'], " free", newln];

    for 'i' from 1 to list.length[lend 'mine'.items] by 1 {
        check std::print.useful['i', ". ", 'mine'.items[pos 'i'], newln];
    }

    let 'x': bin64 = 0.1;
    std::print.stored['x', newln];
    std::print.useful['x', newln];

    let 'tag' := give 'mine'.owner;
    std::print.stored['tag', newln];
}
Output
#7 (Tankun): 3 stored, 9 free
1. helmet
2. gloves
3. rope
0.1000000000000000055511151231257827021181583404541015625
0.1
Tankun