Library
Module
Module type
Parameter
Class
Class type
Options
val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b
map_or ~default f o
is f x
if o = Some x
, default
otherwise.
val map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b
map_lazy default_fn f o
if f o
if o = Some x
, default_fn ()
otherwise.
val is_some : _ t -> bool
is_some (Some x)
returns true
otherwise it returns false
.
val is_none : _ t -> bool
is_none None
returns true
otherwise it returns false
.
Compare two options, using custom comparators for the value. None
is always assumed to be less than Some _
.
Test for equality between option types using a custom equality predicat.
val return : 'a -> 'a t
Monadic return, that is return x = Some x
.
map2 f o1 o2
maps 'a option
and 'b option
to a 'c option
using f
.
val iter : ('a -> unit) -> 'a t -> unit
Iterate on 0 or 1 element.
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
Fold on 0 or 1 element.
val exists : ('a -> bool) -> 'a t -> bool
Return true
iff there exists an element for which the provided function evaluates to true
.
val for_all : ('a -> bool) -> 'a t -> bool
Return true
iff the provided function evaluates to true
for all elements.
val get_or : default:'a -> 'a t -> 'a
get_or ~default o
extracts the value from o
, or returns default
if o = None
.
val get_exn : 'a t -> 'a
Open the option, possibly failing if it is None
.
val get_lazy : (unit -> 'a) -> 'a t -> 'a
get_lazy default_fn x
unwraps x
, but if x = None
it returns default_fn ()
instead.
sequence_l [x1; x2; ...; xn]
returns Some [y1;y2;...;yn]
if every xi
is Some yi
. Otherwise, if the list contains at least one None
, the result is None
.
wrap f x
calls f x
and returns Some y
if f x = y
. If f x
raises any exception, the result is None
. This can be useful to wrap functions such as Map.S.find
.
wrap2 f x y
is similar to wrap
but for binary functions.
Applicative
f <*> (Some x)
returns Some (f x)
and f <*> None
returns None
.
Alternatives
or_lazy ~else_ a
is a
if a
is Some _
, else_ ()
otherwise.
val return_if : bool -> 'a -> 'a t
Apply Some
or None
depending on a boolean. More precisely, return_if false x
is None
, and return_if true x
is Some x
.
Infix Operators
module Infix : sig ... end
Conversion and IO
val to_list : 'a t -> 'a list
val of_list : 'a list -> 'a t
Head of list, or None
.
val to_result : 'e -> 'a t -> ('a, 'e) Result.result
val to_result_lazy : (unit -> 'e) -> 'a t -> ('a, 'e) Result.result
val of_result : ('a, _) Result.result -> 'a t
type 'a printer = Format.formatter -> 'a -> unit
type 'a random_gen = Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_gen
choice_seq s
is similar to choice
, but works on sequences. It returns the first Some x
occurring in s
, or None
otherwise.