union-type-option
Option / Maybe implementation for union-type. See also union-type-either.
Implemented interfaces:
- Setoid
- Foldable
- Functor
- Apply
- Chain
- Applicative
- Monad
- Extract
Documentation
Like Ramda, the functions in this lib take
the Opt
instance as the final argument. All functions with more than one
argument are auto-curried using ramda.
This library is written in node-supported es2015 (4.0+) so if you're running in an old environment you may need to transpile to es5.
var Opt = var Some = OptSomevar None = OptNone
:: a -> Opt a
Some Create an instance of Opt
with a non-null value.
// Some(1)
:: Unit -> Opt
None Create an instance of Opt
with a null value.
// None()
none
Alias to get the instance of None()
Optnone // None()
:: Opt a -> Opt b -> Boolean
equals Compare the contained value of one Opt
against another using ===
.
Opt //trueOpt //falseOpt //true
:: (a -> b) -> Opt a -> Opt b
map Run a function on a value in an Opt
and return new Opt with the result.
Opt // Some(4)
:: (a -> Boolean) -> Opt a -> Opt a
filter Run a predicate on a value in Opt
, if true the Some()
is returned, else None()
Opt // None()Opt // Some(4)
:: Opt a -> a
extract Get the value out of an Opt
. May be null!
Opt // 1Opt // null
:: a -> Opt b -> a
of Put a value in an Opt
. Mostly useful for higher level operations.
Opt // Some(1)Opt // Some(1)
:: (a -> Opt b) -> Opt a -> Opt b
chain Run a function that returns an Opt
on the value in another Opt
.
var strlength < 8 ? : var /[A-Z]/ ? : var Opt
:: Opt a -> Opt (a -> b) -> Opt b
ap Run a function inside an Opt
on the value in another Opt
Opt // Some(4)
:: (b -> a -> b) -> b -> Opt a -> b
reduce Turn an option into something else by combining its value with a seed and a reducing function.
Opt // Some(3)
:: Opt a => (a -> b) -> a -> Opt b
extend Run a function on a Opt
and wrap result in another Opt
.
Opt // Some(Some(2))