Learn · 07
Collections
Work with typed List<T> and Map<K, V>, higher-order functions (map, filter, reduce), safe map indexing, and nested mutations.
Ochre provides two core built-in generic collections: List<T> (ordered dynamic arrays) and Map<K, V> (ordered key-value maps). Both are statically typed, homogeneous, and backed by Copy-on-Write value semantics.
Lists (List<T>)
Create lists with [...], append with push(list, elem) (which returns a new list), and index with xs[i]:
fn main() {
let primes = [2, 3, 5, 7]
let more = push(primes, 11)
println("primes={primes} (len={len(primes)})")
println("more={more}, contains 11? {contains(more, 11)}")
}
Expected Output
primes=[2, 3, 5, 7] (len=4)
more=[2, 3, 5, 7, 11], contains 11? true
Higher-Order Transforms: map, filter, reduce
Transform collections declaratively without mutating state. The type checker automatically infers closure parameter types from the list element type:
fn main() {
let nums = [1, 2, 3, 4, 5, 6]
let squares = map(nums, (n) => n * n)
let big = filter(squares, (n) => n > 10)
let total = reduce(big, 0, (acc, n) => acc + n)
println("squares={squares}")
println("big={big}, total={total}")
}
Expected Output
squares=[1, 4, 9, 16, 25, 36]
big=[16, 25, 36], total=77
Maps (Map<K, V>)
Create maps with #{ key: value }. Because a key might not exist, indexing a map (m[k]) always returns V? (Option<V>). Combine it with ?? or match:
fn main() {
var scores = #{ "ada": 98, "linus": 92 }
scores["grace"] = 100
let ada = scores["ada"] ?? 0
let unknown = scores["guest"] ?? -1
let has_grace = contains(scores, "grace")
println("ada={ada}, unknown={unknown}")
println("keys={keys(scores)}, has grace? {has_grace}")
}
Expected Output
ada=98, unknown=-1
keys=["ada", "grace", "linus"], has grace? true
Nested Collection Mutation
When declared with var, nested list/map assignments (rows[0][1] = 7, rows[0][1] += 5) work cleanly and preserve Copy-on-Write isolation for any snapshot:
fn main() {
var grid = [[1, 2], [3, 4]]
let before = grid
grid[0][1] = 7
grid[1][0] += 10
println("grid={grid}")
println("before={before}")
}
Expected Output
grid=[[1, 7], [13, 4]]
before=[[1, 2], [3, 4]]
Common Mistakes & Compiler Diagnostics
1. Pushing the wrong element type
fn main() {
let xs = [1, 2, 3]
let ys = push(xs, "four")
}
The compiler catches this statically: cannot push 'Str' into 'List<Int>' — even if push is aliased or xs started as an annotated empty let xs: List<Int> = [].
2. Returning a non-Bool from filter
fn main() {
let xs = [1, 2, 3]
let bad = filter(xs, (x) => x + 1)
}
The compiler reports: 'filter' predicate must return 'Bool', found 'Int'.
Exercise
Given let prices = [12, 5, 25, 8, 30], keep only prices >= 10, apply a +2 shipping fee to each, and sum the total using filter, map, and reduce.
Show Solution
fn main() {
let prices = [12, 5, 25, 8, 30]
let eligible = filter(prices, (p) => p >= 10)
let with_shipping = map(eligible, (p) => p + 2)
let total = reduce(with_shipping, 0, (acc, p) => acc + p)
println("eligible={eligible}, total={total}")
}