Module CCOpt
Options
val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'bmap_or ~default f oisf xifo = Some x,defaultotherwise.- since
- 0.16
val map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'bmap_lazy default_fn f oiff oifo = Some x,default_fn ()otherwise.- since
- 1.2
val is_some : _ t -> boolis_some (Some x)returnstrueotherwise it returnsfalse.
val is_none : _ t -> boolis_none Nonereturnstrueotherwise it returnsfalse.- since
- 0.11
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> intcompare comp o1 o2compares two optionso1ando2, using custom comparatorscompfor the value.Noneis always assumed to be less thanSome _.
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> boolequal p o1 o2tests for equality between option typeso1ando2, using a custom equality predicatep.
val return : 'a -> 'a treturn xis a monadic return, that isreturn x = Some x.
val none : 'a tAlias to
None.- since
- 3.5
val flat_map : ('a -> 'b t) -> 'a t -> 'b tflat_map f ois equivalent tomapfollowed byflatten. Flip version of>>=.
val bind : 'a t -> ('a -> 'b t) -> 'b tbind o fisf vifoisSome v,Noneotherwise. Monadic bind.- since
- 3.0
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c tmap2 f o1 o2maps'a optionand'b optionto a'c optionusingf.
val iter : ('a -> unit) -> 'a t -> unititer f oappliesftoo. Iterate on 0 or 1 element.
val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'afold f init oisf init xifoisSome x, orinitifoisNone. Fold on 0 or 1 element.
val filter : ('a -> bool) -> 'a t -> 'a tfilter f oreturnsSome xifoisSome xandf xistrue, orNoneiff xisfalseor ifoisNone. Filter on 0 or 1 element.- since
- 0.5
val exists : ('a -> bool) -> 'a t -> boolexists f oreturnstrueiff there exists an element for which the provided functionfevaluates totrue.- since
- 0.17
val for_all : ('a -> bool) -> 'a t -> boolfor_all f oreturnstrueiff the provided functionfevaluates totruefor all elements.- since
- 0.17
val get_or : default:'a -> 'a t -> 'aget_or ~default oextracts the value fromo, or returnsdefaultifoisNone.- since
- 0.18
val value : 'a t -> default:'a -> 'avalue o ~defaultis similar to the Stdlib'sOption.valueand toget_or.- since
- 2.8
val get_exn : 'a t -> 'aget_exn oreturnsxifoisSome xor fails ifoisNone.- raises Invalid_argument
if the option is
None.
- deprecated
use
get_exn_orinstead
val get_exn_or : string -> 'a t -> 'aget_exn_or msg oreturnsxifoisSome xor fails withInvalid_argument msgifoisNone.- raises Invalid_argument
if the option is
None.
- since
- 3.4
val get_lazy : (unit -> 'a) -> 'a t -> 'aget_lazy default_fn ounwrapso, but ifoisNoneit returnsdefault_fn ()instead.- since
- 0.6.1
val sequence_l : 'a t list -> 'a list tsequence_l [x1; x2; …; xn]returnsSome [y1; y2; …; yn]if everyxiisSome yi. Otherwise, if the list contains at least oneNone, the result isNone.
val wrap : ?handler:(exn -> bool) -> ('a -> 'b) -> 'a -> 'b optionwrap ?handler f xcallsf xand returnsSome yiff x = y. Iff xraises any exception, the result isNone. This can be useful to wrap functions such asMap.S.find.- parameter handler
the exception handler, which returns
trueif the exception is to be caught.
val wrap2 : ?handler:(exn -> bool) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c optionwrap2 ?handler f x yis similar towrapbut for binary functions.
Applicative
Alternatives
val or_lazy : else_:(unit -> 'a t) -> 'a t -> 'a tor_lazy ~else_ oisoifoisSome _,else_ ()ifoisNone.- since
- 1.2
val return_if : bool -> 'a -> 'a treturn_if b xappliesSomeorNonedepending on the booleanb. More precisely,return_if false xisNone, andreturn_if true xisSome x.- since
- 2.2
Infix Operators
- since
- 0.16
module Infix : sig ... endLet operators on OCaml >= 4.08.0, nothing otherwise
- since
- 2.8
Conversion and IO
val to_list : 'a t -> 'a listto_list oreturns[x]ifoisSome xor the empty list[]ifoisNone.
val of_list : 'a list -> 'a tof_list lreturnsSome x(x being the head of the list l), orNoneiflis the empty list.
val to_result : 'e -> 'a t -> ('a, 'e) Stdlib.resultto_result e oreturnsOk xifoisSome x, orError eifoisNone.- since
- 1.2
val to_result_lazy : (unit -> 'e) -> 'a t -> ('a, 'e) Stdlib.resultto_result_lazy f oreturnsOk xifoisSome xorError fifoisNone.- since
- 1.2
val of_result : ('a, _) Stdlib.result -> 'a tof_result resultreturns an option from aresult.- since
- 1.2
type 'a iter= ('a -> unit) -> unittype 'a gen= unit -> 'a optiontype 'a printer= Stdlib.Format.formatter -> 'a -> unittype 'a random_gen= Stdlib.Random.State.t -> 'a
val random : 'a random_gen -> 'a t random_genval choice_iter : 'a t iter -> 'a tchoice_iter iteris similar tochoice, but works oniter. It returns the firstSome xoccurring initer, orNoneotherwise.- since
- 3.0
val choice_seq : 'a t Stdlib.Seq.t -> 'a tchoice_seq seqworks onSeq.t. It returns the firstSome xoccurring inseq, orNoneotherwise.- since
- 3.0
val to_gen : 'a t -> 'a gento_gen oisoas agen.Some xis the singletongencontainingxandNoneis the emptygen.
val to_seq : 'a t -> 'a Stdlib.Seq.tto_seq oisoas a sequenceSeq.t.Some xis the singleton sequence containingxandNoneis the empty sequence. Same asStdlib.Option.to_seq Renamed fromto_std_seqsince 3.0.- since
- 3.0