soma.cell

Iterating Over Maps

Maps are iterated via .keys(), then accessing each value with .get().

Example 020 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 MapIteration keeps 1 memory slot: inventory (Map<String, Int>, persistent and consistent). Handlers: run (line 21).

Generated from soma describe on this file.

The source

020_map_iteration.cell

// ============================================================================
// 020 — Iterating Over Maps
// ============================================================================
// Maps are iterated via .keys(), then accessing each value with .get().
//
// Run:   soma run 020_map_iteration.cell run
// Expect:
//   Inventory:
//     apple: 50
//     banana: 30
//     cherry: 15
//   Total items: 95
//   Most stocked: apple (50)

cell MapIteration {

    memory {
        inventory: Map<String, Int> [persistent, consistent]
    }

    on run() {
        // Populate the map
        inventory.set("apple", 50)
        inventory.set("banana", 30)
        inventory.set("cherry", 15)

        // Iterate using .keys()
        let keys = inventory.keys()
        print("Inventory:")

        let total = 0
        let best_item = ""
        let best_count = 0

        for key in keys {
            let val = inventory.get(key)
            print("  {key}: {val}")
            total = total + val

            // Track the maximum
            if val > best_count {
                best_count = val
                best_item = key
            }
        }

        print("Total items: {total}")
        print("Most stocked: {best_item} ({best_count})")

        return "done"
    }
}

What the toolchain says

$ soma check 020_map_iteration.cell
✓ All checks passed.
$ soma run 020_map_iteration.cell run
Inventory:
  apple: 50
  banana: 30
  cherry: 15
Total items: 95
Most stocked: apple (50)
done