Cookbook
Copy-paste patterns. Each is complete and doctested elsewhere — start here, then follow the link.
Validate input, keep partial results
Validate a payload into a typed value; sum the valid ones and skip the bad ones with try/recover:
-- Doc cookbook: validate a payload into a typed value, and sum valid ones with
-- partial results (try/recover skips the bad ones). A copy-paste-ready pattern.
intent: "doc cookbook: validate + partial results"
type Order
id: number
total: number
task validate_order(payload)
when not contains(payload, "total")
raise("missing total")
when (payload["total"]) < 0
raise("total must be >= 0")
give Order(payload["id"], payload["total"])
task bad_order()
give validate_order({"id": 2, "total": 0 - 5})
task safe_total(payloads)
let total be 0
each p in payloads
try
let o be validate_order(p)
set total to total + (total of o)
recover err
log "skipped: " + err -- partial results: skip the invalid ones
give total
print("safe_total of mixed orders → " + text(safe_total([{"id": 1, "total": 10}, {"id": 2, "total": 0 - 1}, {"id": 3, "total": 5}])))
test "validate accepts good input and rejects bad"
assert_eq(total of validate_order({"id": 1, "total": 100}), 100)
assert_error(bad_order)
test "partial results: invalid orders are skipped"
assert_eq(safe_total([{"id": 1, "total": 10}, {"id": 2, "total": 0 - 1}, {"id": 3, "total": 5}]), 15)
The other patterns (each fully shown on its page)
- HTTP API with auth + validation —
auth with,expect body {…}, the response helpers → HTTP server. - CRUD over SQL —
db_open/sql_exec/sql(SQLite in-memory in the doctest) → SQL, Mongo & Redis. - One LLM operation —
generate "…" given X, branch onllm_available()→ LLM primitives. - An agent with tools — the
llm_step+call_toolallow-list loop → Tool calling. - A secret in any header —
{"x-api-key": secret("KEY")}/bearer(...), materialized only at the socket → Secrets. - Read/write a file with capabilities —
require file(...)+read_file/write_file→ Files & I/O.
Every snippet on those pages is doctested against this version, so an LLM can copy one and it works.