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) # => 8use 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
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)
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.
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 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
fn const(x: t) -> Function(u, t)
Return a function that ignores its argument and always returns x.
fn error(reason: e) -> Result(t, e)
Build an Error(reason) Result.
fn flat_map_option(opt: Option(t), f: Function(t, Option(u))) -> Option(u)
Monadic bind on Option; short-circuits on None.
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 map_error(result: Result(t, e), f: Function(e, b)) -> Result(t, b)
Map over the Error arm; passes Ok through unchanged.
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)
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()
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: Function(e, Result(t, b))) -> Result(t, b)
Recover from an Error by calling f on the reason; Ok is
passed through.
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.
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.