Intent
A loop can transform values, select a subset, or accumulate a result. Until we read its body, we don’t know which. map, filter, and reduce put that intent in the name.
An error branch has a similar property:
if err != nil {
// Return it? Add context? Recover? Try again?
}
The conditional tells us when we act. Its body tells us what we intend to do. Some of those intentions recur often enough to deserve names.
What are the filter/map/reduce of failure handling? Not literal equivalents of those three operations, but a small vocabulary that makes common handling policies recognizable.
In Rye, the language I’m developing in Go, I’ve been exploring one possible answer. These patterns aren’t new; the question is how to expose them as functions that read together.
Check, ensure: proceed only if this holds
Consider a port parser in Go, using fmt and strconv:
func parsePort(raw string) (int, error) {
port, err := strconv.Atoi(raw)
if err != nil {
return 0, fmt.Errorf("parse port: %w", err)
}
if port <= 0 {
return 0, fmt.Errorf("port must be positive")
}
if port >= 65536 {
return 0, fmt.Errorf("port must be below 65536")
}
return port, nil
}
There are two recurring decisions here: check an operation’s outcome, and ensure a condition holds. In Rye:
parse-port: fn { raw } {
port: integer raw |^check "parse port"
port > 0 |^ensure "port must be positive"
port < 65536 |^ensure "port must be below 65536"
port
}
| passes the result on its left to the next function. ^check passes success through; on failure, it adds context and returns. ^ensure returns a failure when its condition is false. The final expression supplies the successful result.
Both versions make the same accept/reject decisions. Rye retains the parsing error as a cause, though its error representation differs from Go’s.
There’s a control-flow difference: ^ is a naming convention for returning functions. Rye’s evaluator lets them exit the enclosing function. An ordinary Go helper cannot do that.
Fix: substitute a recovery path
A caller might choose recovery instead of propagation:
port: parse-port "oops" |fix { 8080 }
print port
; prints: 8080
fix leaves success alone. On failure, it runs the recovery block and uses its result, without itself returning from the enclosing function.
Like seeing filter instead of a loop, seeing fix tells us the shape of the decision before we inspect the block: this is a recovery point. The block tells us the particular recovery policy.
That also makes a questionable policy easier to spot. Here we hide malformed configuration behind a default. For a production service, I would usually default only when an optional setting is absent—not when it is invalid.
Fix either: handle both outcomes
At an output boundary, neither outcome should pass through unchanged. We want a handler for each:
parse-port "8080" |fix\either
{ |message? |print }
{ |print }
The first block receives the failure; the second receives the successful value. This example prints the outer error message or the port:
| Input | Output |
|---|---|
"8080" |
8080 |
"oops" |
parse port |
"-1" |
port must be positive |
fix means “recover if needed.” fix\either means “choose a handler for either outcome.” Related operations, but different intentions at the call site.
Retry, timeout: give execution a policy
The vocabulary can also describe how we attempt an operation. With an application-defined fetch-status:
retry 3 { fetch-status }
timeout 5000 { fetch-status }
Try again on failure. Bound the wait. Those intentions would otherwise be spread across loops, counters, timers, and branches. Unconditionally repeating work is a different intention from retrying failed work.
Names don’t replace contracts, though. Rye’s current retry 3 makes up to three total attempts without backoff. timeout takes milliseconds and stops waiting; it does not cancel the underlying work. Whether an operation is safe to retry remains the caller’s responsibility.
A vocabulary, not a ban on branches
The analogy has a limit: collection operations usually transform data, while failure-handling functions may also affect control flow and side effects. Their contracts need to make that clear. Rye’s active-failure and early-return rules are concepts readers must learn.
But the same test seems useful: does the name let us recognize the pattern without reconstructing it from its implementation?
For these examples, the vocabulary is: check an outcome, ensure a condition, recover, handle either outcome, retry, limit the wait. Go’s ordinary branches remain explicit and flexible, just as loops remain useful alongside map and filter.
Which failure-handling decisions deserve that kind of shared name—and which are clearer left as an if?