Support.Option
include module type of Stdlib.Option
val eliminate : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b
eliminate none_handler some_handler o
is some_handler v
if o
is Some v
, and none_handler ()
otherwise.
val get_or_else : (unit -> 'a) -> 'a t -> 'a
get_or_else f o
is f ()
if o
is None
and v
if o
is Some v
.
val of_bool : bool -> unit t
of_bool b
is Some ()
if b
is true
, and None
otherwise.
When used with other monadic operations, this is (a specialized) guard
function from Haskell, which allows to abort a monadic computation on account of a boolean check.
val from_predicate : ('a -> bool) -> 'a -> 'a t
from_predicate p a
is Some a
if p a
, and None
otherwise.
lazy_alt a1 a2
is a1
if it is not lazy None
, and a2
otherwise.
Infix operator alias of lazy_alt
.
val when_some : 'a t -> ('a -> unit) -> unit
Specialized effectful eliminator for option types.
val print :
(Format.formatter -> 'a -> unit) ->
Format.formatter ->
'a t ->
unit
Prints an option by doing nothing if it is None
; else it uses the given printer.
val pp : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit
Prints an option by showing "None"
or "Some (X)"
where "X"
is generated by the given formatting function.
include Monad.MONAD with type 'a t := 'a t
val return : 'a -> 'a t
return a
injects a
into the monadic type.
bind f a
is the sequential composition of two actions, passing any value produced by a
as argument to f
.
compose g f
is the Kleisli composition of f
and g
, passing the input to f
, then binding the output to g
.
( let* ) ma f
is bind f ma
. This is a binding operator, and it is used as let* a = ma in f a
.
include Functor.FUNCTOR with type 'a t := 'a t
map f
is the function that maps values of t
by f
. The order of arguments is for use in function pipelines as fb = fa |> map (fun a -> (* ... *))
.
( let+ ) ma f
is map f a
. This is a binding operator, and is used as let+ a = ma in f a
include Apply.APPLY with type 'a t := 'a t
ap fa fab
applies argument fa
to fab
under the abstract datatype t
.
ap_first second first
combines actions first
and second
but keeps only first
. That is, ap_first second first = first
. The order of arguments is for use in function pipelines as first = first |> ap_first second
.
ap_second second first
combines actions first
and second
but keeps only second
. That is, ap_second second first = second
. The order of arguments is for use in function pipelines as second = first |> ap_second second
.
seq2 fa1 fa2
sequentially executes actions fa1
and fa2
, and keeps their outputs under the abstract datatype t
.
seq3 fa1 fa2 fa3
sequentially executes actions fa1
, fa2
and fa3
, and keeps their outputs under the abstract datatype t
.
seq4 fa1 fa2 fa3 fa4
sequentially executes actions fa1
, fa2
, fa3
and fa4
, and keeps their outputs under the abstract datatype t
.
seq5 fa1 fa2 fa3 fa4 fa5
sequentially executes actions fa1
, fa2
, fa3
, fa4
and fa5
, and keeps their outputs under the abstract datatype t
.
lift2 f ma1 ma2
sequentially executes actions ma1
, ma2
and passes their outputs to f
.
lift3 f ma1 ma2 ma3
sequentially executes actions ma1
, ma2
, ma3
and passes their outputs to f
.
lift4 f ma1 ma2 ma3 ma4
sequentially executes actions ma1
, ma2
, ma3
, ma4
and passes their outputs to f
.