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_err | (Result(a, b), Fn(b) -> c) -> Result(a, c) | Transform the error |
map_ok | (Result(a, b), Fn(a) -> c) -> Result(c, b) | Transform the success value |
unwrap_or | (Result(a, b), a) -> a | Extract value or use default |
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_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.message())
}
}open in playgroundresult.map_ok
result.map_ok(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_ok { n -> n * 2 }
println(r) -- Ok(42)
}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.
Summary
| Function | Signature | Description |
|---|---|---|
flat_map | (Option(a), Fn(a) -> Option(b)) -> Option(b) | Chain optional operations |
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 |
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.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