Enumerate
enumerate() adds an index to each element of a list.
Example 026 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell Enumerate has no face and no memory. Handlers: run (line 15).
The source
026_enumerate.cell
// ============================================================================
// 026 — Enumerate
// ============================================================================
// enumerate() adds an index to each element of a list.
//
// Run: soma run 026_enumerate.cell run
// Expect:
// 0: apple
// 1: banana
// 2: cherry
// 3: date
cell Enumerate {
on run() {
let fruits = list("apple", "banana", "cherry", "date")
// enumerate() returns a list of maps with "index" and "value" fields
let indexed = enumerate(fruits)
for item in indexed {
print("{item.index}: {item.value}")
}
return "done"
}
}What the toolchain says
$ soma check 026_enumerate.cell
✓ All checks passed.$ soma run 026_enumerate.cell run
0: apple
1: banana
2: cherry
3: date
done