result

Functions for transforming and querying Result(a, e) values without pattern matching.

Summary

FunctionSignatureDescription
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)) -> BoolTrue if Err
is_ok(Result(a, b)) -> BoolTrue 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) -> aExtract 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 playground

result.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 playground

result.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 playground

result.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 playground

result.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 playground

Works 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 playground

result.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 playground

result.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 playground

option

Functions for transforming and querying Option(a) values without pattern matching.

Summary

FunctionSignatureDescription
flat_map(Option(a), Fn(a) -> Option(b)) -> Option(b)Chain optional operations
is_none(Option(a)) -> BoolTrue if None
is_some(Option(a)) -> BoolTrue 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) -> aExtract 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 playground

option.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 playground

option.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 playground

option.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 playground

option.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 playground

option.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