Map Basics
Creating maps, accessing fields, and using with/without/merge.
Example 022 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 MapBasics has no face and no memory. Handlers: run (line 17).
The source
022_map_basics.cell
// ============================================================================
// 022 — Map Basics
// ============================================================================
// Creating maps, accessing fields, and using with/without/merge.
//
// Run: soma run 022_map_basics.cell run
// Expect:
// person: {"name": "Alice", "age": 30, "city": "Paris"}
// name: Alice
// age: 30
// added: {"name": "Alice", "age": 30, "city": "Paris", "email": "[email protected]"}
// removed: {"name": "Alice", "city": "Paris", "email": "[email protected]"}
// merged: {"a": 1, "b": 2, "c": 3, "d": 4}
cell MapBasics {
on run() {
// map() creates a map with alternating key-value pairs
let person = map("name", "Alice", "age", 30, "city", "Paris")
print("person: {person}")
// Access fields with dot notation
print("name: {person.name}")
print("age: {person.age}")
// with() adds or updates a key (returns new map)
let updated = with(person, "email", "[email protected]")
print("added: {updated}")
// without() removes a key (returns new map)
let smaller = without(updated, "age")
print("removed: {smaller}")
// merge() combines two maps (right wins on conflicts)
let m1 = map("a", 1, "b", 2)
let m2 = map("c", 3, "d", 4)
let combined = merge(m1, m2)
print("merged: {combined}")
return "done"
}
}What the toolchain says
$ soma check 022_map_basics.cell
✓ All checks passed.$ soma run 022_map_basics.cell run
person: {"name": "Alice", "age": 30, "city": "Paris"}
name: Alice
age: 30
added: {"name": "Alice", "age": 30, "city": "Paris", "email": "[email protected]"}
removed: {"name": "Alice", "city": "Paris", "email": "[email protected]"}
merged: {"a": 1, "b": 2, "c": 3, "d": 4}
done