result
Functions for transforming and querying Result(a, e) values without pattern
matching.
Summary
| Function | Signature | Description |
|---|---|---|
flat_map | (Result(a, b), Fn(a) -> Result(c, b)) -> Result(c, b) | Chain fallible operations |
flatten | (Result(Result(a, b), b)) -> Result(a, b) | Remove one nesting level |
is_err | (Result(a, b)) -> Bool | True if Err |
is_ok | (Result(a, b)) -> Bool | True if Ok |
map | (Result(a, b), Fn(a) -> c) -> Result(c, b) | Transform the success value |
map_err | (Result(a, b), Fn(b) -> c) -> Result(a, c) | Transform the error |
to_option | (Result(a, b)) -> Option(a) | Some of the success value, None for an error |
unwrap_or | (Result(a, b), a) -> a | Extract value or use default |
Functor, Applicative and Monad
Result(_, e), a result with its error type fixed, has the three
built-in traits of type constructors (see
Functors): Functor.map is result.map,
Monad.flat_map is result.flat_map, Result.pure(x) is Ok(x), and
Applicative.map2(a, b, f) is Ok(f(x, y)) of two Oks, else the first
Err, the left one first:
fn main() {
let a: Result(Int, String) = Ok(1)
let b: Result(Int, String) = Err("no b")
let c: Result(Int, String) = Err("no c")
println(Applicative.map2(a, a) { x, y -> x + y }) -- Ok(2)
println(Applicative.map2(b, c) { x, y -> x + y }) -- Err("no b")
println(Result.pure(1) |> Functor.map { x -> x + 1 }) -- Ok(2)
}open in playgroundThe traits are for generic code; code that knows it has a result writes
result.map and Ok(x).
result.flat_map
result.flat_map(r: Result(a, b), f: Fn(a) -> Result(c, b)) -> Result(c, b)
If r is Ok(v), calls f(v) and returns its result. If r is Err,
returns the Err unchanged. Useful for chaining fallible operations.
import int
import result
fn main() {
let r = Ok("42") |> result.flat_map { s -> int.parse(s) }
println(r) -- Ok(42)
}open in playgroundresult.flatten
result.flatten(r: Result(Result(a, b), b)) -> Result(a, b)
Collapses a nested Result. Ok(Ok(v)) becomes Ok(v), Ok(Err(e)) becomes
Err(e), and Err(e) stays Err(e).
import result
fn main() {
println(result.flatten(Ok(Ok(42)))) -- Ok(42)
println(result.flatten(Ok(Err("oops")))) -- Err("oops")
}open in playgroundresult.is_err
result.is_err(r: Result(a, b)) -> Bool
Returns true if the result is an Err.
import result
fn main() {
println(result.is_err(Err("fail"))) -- true
println(result.is_err(Ok(42))) -- false
}open in playgroundresult.is_ok
result.is_ok(r: Result(a, b)) -> Bool
Returns true if the result is an Ok.
import result
fn main() {
println(result.is_ok(Ok(42))) -- true
println(result.is_ok(Err("fail"))) -- false
}open in playgroundresult.map
result.map(r: Result(a, b), f: Fn(a) -> c) -> Result(c, b)
If r is Ok(v), returns Ok(f(v)). If r is Err, returns it unchanged.
import result
fn main() {
let r = Ok(21) |> result.map { n -> n * 2 }
println(r) -- Ok(42)
}open in playgroundresult.map_err
result.map_err(r: Result(a, b), f: Fn(b) -> c) -> Result(a, c)
If r is Err(e), returns Err(f(e)). If r is Ok, returns it unchanged.
import result
fn main() {
let r = Err("not found") |> result.map_err { e -> "Error: {e}" }
println(r) -- Err("Error: not found")
}open in playgroundWorks well with a variant constructor as the mapping function. Silt
treats a one-field variant constructor as a first-class Fn(e) -> Wrap,
so result.map_err(r, Wrap) lifts a module-specific error into a
caller-owned enum without a closure. A ? that ends a pipeline applies
to the whole pipeline, so a pipe followed by ? composes without
parentheses:
import io
import result
type AppError {
IoWrap(io.IoError),
}
fn load(path: String) -> Result(String, AppError) {
let raw = io.read_file(path) |> result.map_err(IoWrap)?
Ok(raw)
}
fn main() {
match load("missing.txt") {
Ok(s) -> println(s)
Err(IoWrap(e)) -> println("{e}")
}
}open in playgroundresult.to_option
result.to_option(r: Result(a, b)) -> Option(a)
Some(v) for Ok(v), None for an Err: the error is dropped. The
reverse of option.to_result.
import int
import result
fn main() {
println(result.to_option(int.parse("42"))) -- Some(42)
println(result.to_option(int.parse("x"))) -- None
}open in playgroundresult.unwrap_or
result.unwrap_or(r: Result(a, b), default: a) -> a
Returns the Ok value, or default if the result is Err.
import result
fn main() {
println(result.unwrap_or(Ok(42), 0)) -- 42
println(result.unwrap_or(Err("fail"), 0)) -- 0
}open in playgroundoption
Functions for transforming and querying Option(a) values without pattern
matching.
Options are ordered as any enum is, by their variants in the order they
are declared and then by their payloads: every Some(x) comes before
None, so a sort puts the Nones last. (So do results: Ok(x) before
Err(e).)
import list
fn main() {
println(list.sort([None, Some(2), Some(1)])) -- [Some(1), Some(2), None]
println(Some(1) < None) -- true
}open in playgroundSummary
| Function | Signature | Description |
|---|---|---|
flat_map | (Option(a), Fn(a) -> Option(b)) -> Option(b) | Chain optional operations |
flatten | (Option(Option(a))) -> Option(a) | Remove one nesting level |
is_none | (Option(a)) -> Bool | True if None |
is_some | (Option(a)) -> Bool | True if Some |
map | (Option(a), Fn(a) -> b) -> Option(b) | Transform the inner value |
to_result | (Option(a), b) -> Result(a, b) | Convert to Result with error value |
unwrap_or | (Option(a), a) -> a | Extract value or use default |
Functor, Applicative and Monad
Option(_) has the three built-in traits of type constructors (see
Functors): Functor.map is option.map,
Monad.flat_map is option.flat_map, Option.pure(x) is Some(x),
and Applicative.map2(a, b, f) is Some(f(x, y)) of two Somes, else
None:
fn main() {
println(Applicative.map2(Some(1), Some(2)) { x, y -> x + y }) -- Some(3)
println(Applicative.map2(Some(1), None) { x, y -> x + y }) -- None
println(Option.pure(1)) -- Some(1)
}open in playgroundThe traits are for generic code; code that knows it has an option writes
option.map and Some(x).
option.flat_map
option.flat_map(opt: Option(a), f: Fn(a) -> Option(b)) -> Option(b)
If opt is Some(v), calls f(v) and returns its result. If opt is None,
returns None.
import option
fn main() {
let chained = Some(42)
|> option.flat_map { n ->
match {
n > 0 -> Some(n * 2)
_ -> None
}
}
println(chained) -- Some(84)
}open in playgroundoption.flatten
option.flatten(opt: Option(Option(a))) -> Option(a)
Collapses a nested Option. Some(Some(v)) becomes Some(v), and
Some(None) and None become None.
import option
fn main() {
println(option.flatten(Some(Some(42)))) -- Some(42)
println(option.flatten(Some(None))) -- None
}open in playgroundoption.is_none
option.is_none(opt: Option(a)) -> Bool
Returns true if the option is None.
import option
fn main() {
println(option.is_none(None)) -- true
println(option.is_none(Some(1))) -- false
}open in playgroundoption.is_some
option.is_some(opt: Option(a)) -> Bool
Returns true if the option is Some.
import option
fn main() {
println(option.is_some(Some(1))) -- true
println(option.is_some(None)) -- false
}open in playgroundoption.map
option.map(opt: Option(a), f: Fn(a) -> b) -> Option(b)
If opt is Some(v), returns Some(f(v)). If opt is None, returns None.
import option
fn main() {
let doubled = Some(21) |> option.map { n -> n * 2 }
println(doubled) -- Some(42)
}open in playgroundoption.to_result
option.to_result(opt: Option(a), error: b) -> Result(a, b)
Converts Some(v) to Ok(v) and None to Err(error).
import option
fn main() {
let r = option.to_result(Some(42), "missing")
println(r) -- Ok(42)
let r2 = option.to_result(None, "missing")
println(r2) -- Err("missing")
}open in playgroundoption.unwrap_or
option.unwrap_or(opt: Option(a), default: a) -> a
Returns the inner value if Some, otherwise returns default.
import option
fn main() {
println(option.unwrap_or(Some(42), 0)) -- 42
println(option.unwrap_or(None, 0)) -- 0
}open in playground