Sum, Avg, Min, Max
Convenience aggregation functions that work on a whole list.
Example 035 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 Aggregates has no face and no memory. Handlers: run (line 15).
The source
035_sum_avg_min_max.cell
// ============================================================================
// 035 — Sum, Avg, Min, Max
// ============================================================================
// Convenience aggregation functions that work on a whole list.
//
// Run: soma run 035_sum_avg_min_max.cell run
// Expect:
// Total revenue: 520
// Average revenue: 104
// Lowest: {"region": "East", "revenue": 45}
// Highest: {"region": "West", "revenue": 200}
cell Aggregates {
on run() {
let sales = list(
map("region", "West", "revenue", 200),
map("region", "East", "revenue", 45),
map("region", "North", "revenue", 150),
map("region", "South", "revenue", 125)
)
// sum_by sums a numeric field across all rows
let total = sales |> sum_by("revenue")
print("Total revenue: {total}")
// avg_by computes the mean
let average = sales |> avg_by("revenue")
print("Average revenue: {average}")
// min_by / max_by return the entire row with the min/max value
let lowest = sales |> min_by("revenue")
print("Lowest: {lowest}")
let highest = sales |> max_by("revenue")
print("Highest: {highest}")
return "done"
}
}What the toolchain says
$ soma check 035_sum_avg_min_max.cell
✓ All checks passed.$ soma run 035_sum_avg_min_max.cell run
Total revenue: 520
Average revenue: 130
Lowest: {"region": "East", "revenue": 45}
Highest: {"region": "West", "revenue": 200}
done