Std.Core
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
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
Okarm; short-circuits onError. Chain fallible steps asand_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 asf(x), but passingapplyitself 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
valueinto the inclusive range[lo, hi]. -
# fn compose(f: (B) -> C, g: (A) -> B) -> (A) -> C
Right-to-left function composition.
compose(f, g)(x)isf(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 onNone. -
# 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
truewhen the result is in theErrorarm. -
# fn is_none(opt: Option(T)) -> Bool
truewhen the option is empty. -
# fn is_ok(result: Result(T, E)) -> Bool
truewhen the result is in theOkarm. -
# fn is_some(opt: Option(T)) -> Bool
truewhen 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
Errorarm; passesOkthrough unchanged. -
# fn map_ok(result: Result(T, E), f: (T) -> U) -> Result(U, E)
Map over the
Okarm; passesErrorthrough 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
Errorby callingfon the reason;Okis 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 todefaultonNone. -
# fn unwrap_error(result: Result(T, E), default: E) -> E
Extract the reason from an
Error, falling back todefaultonOk. -
# fn unwrap_ok(result: Result(T, E), default: T) -> T
Extract the value from an
Ok, falling back todefaultonError.