Aggregation with agg()
agg() groups by a field and computes aggregates (sum, avg, count, etc).
Example 034 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 Aggregation has no face and no memory. Handlers: run (line 15).
The source
034_aggregation.cell
// ============================================================================
// 034 — Aggregation with agg()
// ============================================================================
// agg() groups by a field and computes aggregates (sum, avg, count, etc).
//
// Run: soma run 034_aggregation.cell run
// Expect:
// Department summary:
// Eng: 2 people, total salary 210k, avg 105k
// Sales: 2 people, total salary 170k, avg 85k
// Design: 1 people, total salary 95k, avg 95k
cell Aggregation {
on run() {
let employees = list(
map("dept", "Eng", "name", "Alice", "salary", 120),
map("dept", "Eng", "name", "Bob", "salary", 90),
map("dept", "Sales", "name", "Carol", "salary", 85),
map("dept", "Sales", "name", "Dave", "salary", 85),
map("dept", "Design", "name", "Eve", "salary", 95)
)
// agg(list, group_by_field, "col:func", ...)
// Available functions: sum, avg, count, min, max
let summary = employees
|> agg("dept", "salary:sum", "salary:avg")
print("Department summary:")
for row in summary {
print(" {row.dept}: {row.count} people, total salary {row.salary_sum}k, avg {row.salary_avg}k")
}
return "done"
}
}What the toolchain says
$ soma check 034_aggregation.cell
✓ All checks passed.$ soma run 034_aggregation.cell run
Department summary:
Eng: 2 people, total salary 210k, avg 105k
Sales: 2 people, total salary 170k, avg 85k
Design: 1 people, total salary 95k, avg 95k
done