soma.cell

Nested Data Structures

Lists of maps, maps of lists — complex nested data.

Example 023 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 NestedData has no face and no memory. Handlers: run (line 15).

Generated from soma describe on this file.

The source

023_nested_data.cell

// ============================================================================
// 023 — Nested Data Structures
// ============================================================================
// Lists of maps, maps of lists — complex nested data.
//
// Run:   soma run 023_nested_data.cell run
// Expect:
//   Alice is 30, from Paris
//   Bob is 25, from London
//   Carol is 35, from Tokyo
//   Total age: 90

cell NestedData {

    on run() {
        // A list of maps — like a table or array of records
        let people = list(
            map("name", "Alice", "age", 30, "city", "Paris"),
            map("name", "Bob", "age", 25, "city", "London"),
            map("name", "Carol", "age", 35, "city", "Tokyo")
        )

        // Iterate over the list and access map fields
        let total_age = 0
        for person in people {
            print("{person.name} is {person.age}, from {person.city}")
            total_age = total_age + person.age
        }
        print("Total age: {total_age}")

        return "done"
    }
}

What the toolchain says

$ soma check 023_nested_data.cell
✓ All checks passed.
$ soma run 023_nested_data.cell run
Alice is 30, from Paris
Bob is 25, from London
Carol is 35, from Tokyo
Total age: 90
done