Filter and Sort Pipelines
filter_by() and sort_by() are the building blocks of data pipelines.
Example 031 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 FilterSort has no face and no memory. Handlers: run (line 21).
The source
031_filter_sort.cell
// ============================================================================
// 031 — Filter and Sort Pipelines
// ============================================================================
// filter_by() and sort_by() are the building blocks of data pipelines.
//
// Run: soma run 031_filter_sort.cell run
// Expect:
// Expensive items (price > 50):
// Laptop: 999
// Phone: 699
// Tablet: 449
// Sorted by price ascending:
// Mouse: 29
// Keyboard: 49
// Tablet: 449
// Phone: 699
// Laptop: 999
cell FilterSort {
on run() {
let products = list(
map("name", "Laptop", "price", 999),
map("name", "Mouse", "price", 29),
map("name", "Keyboard", "price", 49),
map("name", "Phone", "price", 699),
map("name", "Tablet", "price", 449)
)
// filter_by(list, field, operator, value)
// Operators: "=", "!=", ">", "<", ">=", "<="
let expensive = filter_by(products, "price", ">", 50)
print("Expensive items (price > 50):")
for item in expensive {
print(" {item.name}: {item.price}")
}
// sort_by(list, field, direction)
// Direction: "asc" or "desc"
let sorted = sort_by(products, "price", "asc")
print("Sorted by price ascending:")
for item in sorted {
print(" {item.name}: {item.price}")
}
return "done"
}
}What the toolchain says
$ soma check 031_filter_sort.cell
✓ All checks passed.$ soma run 031_filter_sort.cell run
Expensive items (price > 50):
Laptop: 999
Phone: 699
Tablet: 449
Sorted by price ascending:
Mouse: 29
Keyboard: 49
Tablet: 449
Phone: 699
Laptop: 999
done