Recipe Book
Store and search recipes with ingredients.
Example 088 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 RecipeBook has no face and no memory. Handlers: demo (line 16).
The source
088_recipe_book.cell
// ============================================================================
// 088 — Recipe Book
// ============================================================================
// Store and search recipes with ingredients.
//
// Run: soma run 088_recipe_book.cell demo
// Expect:
// Added: Pasta Carbonara
// Added: Caesar Salad
// Added: Chocolate Cake
// All recipes: 3
// Search 'egg': Pasta Carbonara, Chocolate Cake
cell RecipeBook {
on demo() {
let recipes = list(
map("name", "Pasta Carbonara",
"ingredients", "pasta, eggs, bacon, parmesan, pepper",
"time", 20),
map("name", "Caesar Salad",
"ingredients", "romaine, croutons, parmesan, dressing",
"time", 10),
map("name", "Chocolate Cake",
"ingredients", "flour, sugar, cocoa, eggs, butter, milk",
"time", 45)
)
for r in recipes {
print("Added: {r.name}")
}
let count = recipes.length
print("All recipes: {count}")
// Search by ingredient
let query = "egg"
let results = ""
for r in recipes {
let has = contains(lowercase(r.ingredients), query)
if has == true {
if results != "" {
results += ", "
}
results += r.name
}
}
print("Search '{query}': {results}")
return "done"
}
}What the toolchain says
$ soma check 088_recipe_book.cell
warning: `results` grows by concatenation inside a loop — each step copies the whole value, so the loop is quadratic (8 MB took 32 s, a list + join() 30 ms): push the pieces to a List and `join(parts, "")` once
--> 088_recipe_book.cell:43:21
|
43 | results += ", "
| ^
✓ 1 warning(s), no errors.$ soma run 088_recipe_book.cell demo
Added: Pasta Carbonara
Added: Caesar Salad
Added: Chocolate Cake
All recipes: 3
Search 'egg': Pasta Carbonara, Chocolate Cake
done