Std.Core (Cure Standard Library v0.37.2)

View Source

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

let positive = fn(x: Int) -> Result(Int, Atom) =
  pickup
    x > 0 -> ok(x)
    else  -> error(:neg)

ok(42)
  |> map_ok(fn(x) -> x * 2)
  |> and_then(positive)
  |> unwrap_ok(0)                 # => 84

Summary

Functions

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

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.

Short-circuit logical AND.

Logical negation.

Short-circuit logical OR.

Logical exclusive-or.

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

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

Build an Error(reason) Result.

Monadic bind on Option; short-circuits on None.

Identity function: returns its argument unchanged.

true when the result is in the Error arm.

true when the option is empty.

true when the result is in the Ok arm.

true when the option holds a value.

Map over the Error arm; passes Ok through unchanged.

Map over the Ok arm; passes Error through unchanged.

Functorial map on Option. None() is preserved.

The empty Option.

Build an Ok(value) Result.

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

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

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

Build a Some(value) Option.

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

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

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

Functions

and_then(result: Result(t, e), f: Function(t, Result(u, e))) -> Result(u, e)

 fn and_then(result: Result(t, e), f: Function(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

use Std.Core

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)

apply(f: Function(t, u), x: t) -> u

 fn apply(f: Function(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.

bool_and(x: Bool, y: Bool) -> Bool

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

Short-circuit logical AND.

bool_not(x: Bool) -> Bool

 fn bool_not(x: Bool) -> Bool

Logical negation.

bool_or(x: Bool, y: Bool) -> Bool

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

Short-circuit logical OR.

bool_xor(x: Bool, y: Bool) -> Bool

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

Logical exclusive-or.

compose(f: Function(b, c), g: Function(a, b)) -> Function(a, c)

 fn compose(f: Function(b, c), g: Function(a, b)) -> Function(a, c)

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

Examples

use Std.Core

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

const(x: t) -> Function(u, t)

 fn const(x: t) -> Function(u, t)

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

error(reason: e) -> Result(t, e)

 fn error(reason: e) -> Result(t, e)

Build an Error(reason) Result.

flat_map_option(opt: Option(t), f: Function(t, Option(u))) -> Option(u)

 fn flat_map_option(opt: Option(t), f: Function(t, Option(u))) -> Option(u)

Monadic bind on Option; short-circuits on None.

identity(x: t) -> t

 fn identity(x: t) -> t

Identity function: returns its argument unchanged.

is_error(result: Result(t, e)) -> Bool

 fn is_error(result: Result(t, e)) -> Bool

true when the result is in the Error arm.

is_none(opt: Option(t)) -> Bool

 fn is_none(opt: Option(t)) -> Bool

true when the option is empty.

is_ok(result: Result(t, e)) -> Bool

 fn is_ok(result: Result(t, e)) -> Bool

true when the result is in the Ok arm.

is_some(opt: Option(t)) -> Bool

 fn is_some(opt: Option(t)) -> Bool

true when the option holds a value.

map_error(result: Result(t, e), f: Function(e, b)) -> Result(t, b)

 fn map_error(result: Result(t, e), f: Function(e, b)) -> Result(t, b)

Map over the Error arm; passes Ok through unchanged.

map_ok(result: Result(t, e), f: Function(t, u)) -> Result(u, e)

 fn map_ok(result: Result(t, e), f: Function(t, u)) -> Result(u, e)

Map over the Ok arm; passes Error through unchanged.

Examples

use Std.Core

map_ok(ok(3),     fn(x) -> x * x) # => Ok(9)
map_ok(error(:x), fn(x) -> x * x) # => Error(:x)

map_option(opt: Option(t), f: Function(t, u)) -> Option(u)

 fn map_option(opt: Option(t), f: Function(t, u)) -> Option(u)

Functorial map on Option. None() is preserved.

Examples

use Std.Core

map_option(some(3), fn(x) -> x * 2)  # => Some(6)
map_option(none(),  fn(x) -> x * 2)  # => None()

none() -> Option(t)

 fn none() -> Option(t)

The empty Option.

ok(value: t) -> Result(t, e)

 fn ok(value: t) -> Result(t, e)

Build an Ok(value) Result.

option_or(opt: Option(t), default: t) -> t

 fn option_or(opt: Option(t), default: t) -> t

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

or_else(result: Result(t, e), f: Function(e, Result(t, b))) -> Result(t, b)

 fn or_else(result: Result(t, e), f: Function(e, Result(t, b))) -> Result(t, b)

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

pipe(x: t, f: Function(t, u)) -> u

 fn pipe(x: t, f: Function(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.

some(value: t) -> Option(t)

 fn some(value: t) -> Option(t)

Build a Some(value) Option.

unwrap(opt: Option(t), default: t) -> t

 fn unwrap(opt: Option(t), default: t) -> t

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

unwrap_error(result: Result(t, e), default: e) -> e

 fn unwrap_error(result: Result(t, e), default: e) -> e

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

unwrap_ok(result: Result(t, e), default: t) -> t

 fn unwrap_ok(result: Result(t, e), default: t) -> t

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