Identity, composition, booleans, comparison, and the Result / Option sum types.

These are the most commonly used combinators in Cure programs. Result(T, E) and Option(T) lower to {:ok, v} / {:error, e} and {:some, v} / {:none} respectively so they round-trip with Elixir and Erlang without wrappers.

Examples

use Std.Core

let inc    = fn(x: Int) -> Int = x + 1
let double = fn(x: Int) -> Int = x * 2
let pipeline = compose(double, inc)
pipeline(3)                       # => 8
use Std.Core

ok(42)
  |> map_ok(fn(x) -> x * 2)
  |> and_then(fn(x) -> if x > 0 then ok(x) else error(:neg))
  |> unwrap_ok(0)                 # => 84

Functions

  • # fn __group__() -> Atom

    Group tag consumed by Cure.Stdlib.Preload.

  • # fn and_then(result: Result(T, E), f: (T) -> Result(U, E)) -> Result(U, E)

    Monadic bind over the Ok arm; short-circuits on Error. Chain fallible steps as and_then(result, f).

    Examples

    let parse_int = fn(s: String) -> Result(Int, Atom) = ok(Std.String.to_int(s))
    and_then(ok("42"), parse_int)     # => Ok(42)
    and_then(error(:x), parse_int)    # => Error(:x)
    
  • # fn apply(f: (T) -> U, x: T) -> U

    Apply a function to a value. apply(f, x) is the same as f(x), but passing apply itself as a value is sometimes useful.

  • # fn bool_and(x: Bool, y: Bool) -> Bool

    Short-circuit logical AND.

  • # fn bool_not(x: Bool) -> Bool

    Logical negation.

  • # fn bool_or(x: Bool, y: Bool) -> Bool

    Short-circuit logical OR.

  • # fn bool_xor(x: Bool, y: Bool) -> Bool

    Logical exclusive-or.

  • # fn clamp(value: T, lo: T, hi: T) -> T

    Clamp value into the inclusive range [lo, hi].

  • # fn compose(f: (B) -> C, g: (A) -> B) -> (A) -> C

    Right-to-left function composition. compose(f, g)(x) is f(g(x)).

    Examples

    let add1 = fn(x: Int) -> Int = x + 1
    let neg  = fn(x: Int) -> Int = 0 - x
    compose(neg, add1)(3)             # => -4
    
  • # fn const(x: T) -> (U) -> T

    Return a function that ignores its argument and always returns x.

  • # fn eq(x: T, y: T) -> Bool

    Structural equality (x == y).

  • # fn error(reason: E) -> Result(T, E)

    Build an Error(reason) Result.

  • # fn flat_map_option(opt: Option(T), f: (T) -> Option(U)) -> Option(U)

    Monadic bind on Option; short-circuits on None.

  • # fn ge(x: T, y: T) -> Bool

    Greater-than-or-equal (x >= y).

  • # fn gt(x: T, y: T) -> Bool

    Strict greater-than (x > y).

  • # fn identity(x: T) -> T

    Identity function: returns its argument unchanged.

  • # fn is_error(result: Result(T, E)) -> Bool

    true when the result is in the Error arm.

  • # fn is_none(opt: Option(T)) -> Bool

    true when the option is empty.

  • # fn is_ok(result: Result(T, E)) -> Bool

    true when the result is in the Ok arm.

  • # fn is_some(opt: Option(T)) -> Bool

    true when the option holds a value.

  • # fn le(x: T, y: T) -> Bool

    Less-than-or-equal (x <= y).

  • # fn lt(x: T, y: T) -> Bool

    Strict less-than (x < y).

  • # fn map_error(result: Result(T, E), f: (E) -> F) -> Result(T, F)

    Map over the Error arm; passes Ok through unchanged.

  • # fn map_ok(result: Result(T, E), f: (T) -> U) -> Result(U, E)

    Map over the Ok arm; passes Error through unchanged.

    Examples

    map_ok(ok(3),     fn(x) -> x * x) # => Ok(9)
    map_ok(error(:x), fn(x) -> x * x) # => Error(:x)
    
  • # fn map_option(opt: Option(T), f: (T) -> U) -> Option(U)

    Functorial map on Option. None() is preserved.

    Examples

    map_option(some(3), fn(x) -> x * 2)  # => Some(6)
    map_option(none(),  fn(x) -> x * 2)  # => None()
    
  • # fn max(x: T, y: T) -> T

    Pointwise maximum using the primitive >= ordering.

  • # fn min(x: T, y: T) -> T

    Pointwise minimum using the primitive <= ordering.

  • # fn ne(x: T, y: T) -> Bool

    Structural inequality (x != y).

  • # fn none() -> Option(T)

    The empty Option.

  • # fn ok(value: T) -> Result(T, E)

    Build an Ok(value) Result.

  • # fn option_or(opt: Option(T), default: T) -> T

    Unwrap-or-default; an alias used by Std.Match.

  • # fn or_else(result: Result(T, E), f: (E) -> Result(T, F)) -> Result(T, F)

    Recover from an Error by calling f on the reason; Ok is passed through.

  • # fn pipe(x: T, f: (T) -> U) -> U

    Apply a function to a value, reads left-to-right; flip of apply/2. Useful to express |>-style pipelines as plain calls.

  • # fn some(value: T) -> Option(T)

    Build a Some(value) Option.

  • # fn unwrap(opt: Option(T), default: T) -> T

    Extract the value from a Some, falling back to default on None.

  • # fn unwrap_error(result: Result(T, E), default: E) -> E

    Extract the reason from an Error, falling back to default on Ok.

  • # fn unwrap_ok(result: Result(T, E), default: T) -> T

    Extract the value from an Ok, falling back to default on Error.