Synsema docsENES

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:

cookbook.syn
-- 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)

Every snippet on those pages is doctested against this version, so an LLM can copy one and it works.