Module CCOpt
Options
val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b
map_or ~default f o
isf x
ifo = Some x
,default
otherwise.- since
- 0.16
val map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b
map_lazy default_fn f o
iff o
ifo = Some x
,default_fn ()
otherwise.- since
- 1.2
val is_some : _ t -> bool
is_some (Some x)
returnstrue
otherwise it returnsfalse
.
val is_none : _ t -> bool
is_none None
returnstrue
otherwise it returnsfalse
.- since
- 0.11
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
compare comp o1 o2
compares two optionso1
ando2
, using custom comparatorscomp
for the value.None
is always assumed to be less thanSome _
.
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
equal p o1 o2
tests for equality between option typeso1
ando2
, using a custom equality predicatep
.
val return : 'a -> 'a t
return x
is a monadic return, that isreturn x = Some x
.
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
flat_map f o
is equivalent tomap
followed byflatten
. Flip version of>>=
.
val bind : 'a t -> ('a -> 'b t) -> 'b t
bind o f
isf v
ifo
isSome v
,None
otherwise. Monadic bind.- since
- 3.0
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
map2 f o1 o2
maps'a option
and'b option
to a'c option
usingf
.
val iter : ('a -> unit) -> 'a t -> unit
iter f o
appliesf
too
. Iterate on 0 or 1 element.
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
fold f init o
isf init x
ifo
isSome x
, orinit
ifo
isNone
. Fold on 0 or 1 element.
val filter : ('a -> bool) -> 'a t -> 'a t
filter f o
returnsSome x
iff (Some x)
istrue
, orNone
iff (Some x)
isfalse
or ifo
isNone
. Filter on 0 or 1 element.- since
- 0.5
val exists : ('a -> bool) -> 'a t -> bool
exists f o
returnstrue
iff there exists an element for which the provided functionf
evaluates totrue
.- since
- 0.17
val for_all : ('a -> bool) -> 'a t -> bool
for_all f o
returnstrue
iff the provided functionf
evaluates totrue
for all elements.- since
- 0.17
val get_or : default:'a -> 'a t -> 'a
get_or ~default o
extracts the value fromo
, or returnsdefault
ifo
isNone
.- since
- 0.18
val value : 'a t -> default:'a -> 'a
value o ~default
is similar to the Stdlib'sOption.value
and toget_or
.- since
- 2.8
val get_exn : 'a t -> 'a
get_exn o
returnsx
ifo
isSome x
or fails ifo
isNone
.- raises Invalid_argument
if the option is
None
.
val get_lazy : (unit -> 'a) -> 'a t -> 'a
get_lazy default_fn o
unwrapso
, but ifo
isNone
it returnsdefault_fn ()
instead.- since
- 0.6.1
val sequence_l : 'a t list -> 'a list t
sequence_l [x1; x2; …; xn]
returnsSome [y1; y2; …; yn]
if everyxi
isSome yi
. Otherwise, if the list contains at least oneNone
, the result isNone
.
val wrap : ?handler:(exn -> bool) -> ('a -> 'b) -> 'a -> 'b option
wrap ?handler f x
callsf x
and returnsSome y
iff x = y
. Iff x
raises any exception, the result isNone
. This can be useful to wrap functions such asMap.S.find
.- parameter handler
the exception handler, which returns
true
if the exception is to be caught.
val wrap2 : ?handler:(exn -> bool) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c option
wrap2 ?handler f x y
is similar towrap
but for binary functions.
Applicative
Alternatives
val or_lazy : else_:(unit -> 'a t) -> 'a t -> 'a t
or_lazy ~else_ o
iso
ifo
isSome _
,else_ ()
ifo
isNone
.- since
- 1.2
val return_if : bool -> 'a -> 'a t
return_if b x
appliesSome
orNone
depending on the booleanb
. More precisely,return_if false x
isNone
, andreturn_if true x
isSome x
.- since
- 2.2
Infix Operators
- since
- 0.16
module Infix : sig ... end
Let operators on OCaml >= 4.08.0, nothing otherwise
- since
- 2.8
Conversion and IO
val to_list : 'a t -> 'a list
to_list o
returns[x]
ifo
isSome x
or the empty list[]
ifo
isNone
.
val of_list : 'a list -> 'a t
of_list l
returnsSome x
(x being the head of the list l), orNone
ifl
is the empty list.
val to_result : 'e -> 'a t -> ('a, 'e) Stdlib.result
to_result e o
returnsOk x
ifo
isSome x
, orError e
ifo
isNone
.- since
- 1.2
val to_result_lazy : (unit -> 'e) -> 'a t -> ('a, 'e) Stdlib.result
to_result_lazy f o
returnsOk x
ifo
isSome x
orError f
ifo
isNone
.- since
- 1.2
val of_result : ('a, _) Stdlib.result -> 'a t
of_result result
returns an option from aresult
.- since
- 1.2
type 'a iter
= ('a -> unit) -> unit
type 'a gen
= unit -> 'a option
type 'a printer
= Stdlib.Format.formatter -> 'a -> unit
type 'a random_gen
= Stdlib.Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_gen
val choice_iter : 'a t iter -> 'a t
choice_iter iter
is similar tochoice
, but works oniter
. It returns the firstSome x
occurring initer
, orNone
otherwise.- since
- 3.0
val choice_seq : 'a t Stdlib.Seq.t -> 'a t
choice_seq seq
works onSeq.t
. It returns the firstSome x
occurring inseq
, orNone
otherwise.- since
- 3.0
val to_gen : 'a t -> 'a gen
to_gen o
iso
as agen
.Some x
is the singletongen
containingx
andNone
is the emptygen
.
val to_seq : 'a t -> 'a Stdlib.Seq.t
to_seq o
iso
as a sequenceSeq.t
.Some x
is the singleton sequence containingx
andNone
is the empty sequence. Same asStdlib
.Option.to_seq Renamed fromto_std_seq
since 3.0.- since
- 3.0