type 'a t = 'a Base.Option.t
include Bin_prot.Binable.S1 with type 'a t := 'a t
val bin_shape_t : Bin_prot.Shape.t -> Bin_prot.Shape.t
val bin_size_t : ('a, 'a t) Bin_prot.Size.sizer1
val bin_write_t : ('a, 'a t) Bin_prot.Write.writer1
val bin_read_t : ('a, 'a t) Bin_prot.Read.reader1
val __bin_read_t__ : ('a, int -> 'a t) Bin_prot.Read.reader1
val bin_writer_t : ('a, 'a t) Bin_prot.Type_class.S1.writer
val bin_reader_t : ('a, 'a t) Bin_prot.Type_class.S1.reader
val bin_t : ('a, 'a t) Bin_prot.Type_class.S1.t
include Typerep_lib.Typerepable.S1 with type 'a t := 'a t
val typerep_of_t : 'a Typerep_lib.Std_internal.Typerep.t -> 'a t Typerep_lib.Std_internal.Typerep.t
val typename_of_t : 'a Typerep_lib.Typename.t -> 'a t Typerep_lib.Typename.t
include module type of sig ... end with type 'a Option.t := 'a option
include module type of Base.Option with type 'a t := 'a option
val compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int
val hash_fold_t : (Base.Hash.state -> 'a -> Base.Hash.state) -> Base.Hash.state -> 'a option -> Base.Hash.state
include Base.Sexpable.S1 with type 'a t := 'a option
val t_of_sexp : (Sexplib0.Sexp.t -> 'a) -> Sexplib0.Sexp.t -> 'a option
val sexp_of_t : ('a -> Sexplib0.Sexp.t) -> 'a option -> Sexplib0.Sexp.t
val t_sexp_grammar : Base.Sexp.Private.Raw_grammar.t
include Base.Container.S1 with type 'a t := 'a option
val mem : 'a option -> 'a -> equal:('a -> 'a -> bool) -> bool
Checks whether the provided element is there, using
equal
.
val length : 'a option -> int
val is_empty : 'a option -> bool
val iter : 'a option -> f:('a -> unit) -> unit
val fold : 'a option -> init:'accum -> f:('accum -> 'a -> 'accum) -> 'accum
fold t ~init ~f
returnsf (... f (f (f init e1) e2) e3 ...) en
, wheree1..en
are the elements oft
val fold_result : 'a option -> init:'accum -> f:('accum -> 'a -> ('accum, 'e) Base.Result.t) -> ('accum, 'e) Base.Result.t
fold_result t ~init ~f
is a short-circuiting version offold
that runs in theResult
monad. Iff
returns anError _
, that value is returned without any additional invocations off
.
val fold_until : 'a option -> init:'accum -> f:('accum -> 'a -> ('accum, 'final) Base__Container_intf.Export.Continue_or_stop.t) -> finish:('accum -> 'final) -> 'final
fold_until t ~init ~f ~finish
is a short-circuiting version offold
. Iff
returnsStop _
the computation ceases and results in that value. Iff
returnsContinue _
, the fold will proceed. Iff
never returnsStop _
, the final result is computed byfinish
.Example:
type maybe_negative = | Found_negative of int | All_nonnegative of { sum : int } (** [first_neg_or_sum list] returns the first negative number in [list], if any, otherwise returns the sum of the list. *) let first_neg_or_sum = List.fold_until ~init:0 ~f:(fun sum x -> if x < 0 then Stop (Found_negative x) else Continue (sum + x)) ~finish:(fun sum -> All_nonnegative { sum }) ;; let x = first_neg_or_sum [1; 2; 3; 4; 5] val x : maybe_negative = All_nonnegative {sum = 15} let y = first_neg_or_sum [1; 2; -3; 4; 5] val y : maybe_negative = Found_negative -3
val exists : 'a option -> f:('a -> bool) -> bool
Returns
true
if and only if there exists an element for which the provided function evaluates totrue
. This is a short-circuiting operation.
val for_all : 'a option -> f:('a -> bool) -> bool
Returns
true
if and only if the provided function evaluates totrue
for all elements. This is a short-circuiting operation.
val count : 'a option -> f:('a -> bool) -> int
Returns the number of elements for which the provided function evaluates to true.
val sum : (module Base__Container_intf.Summable with type t = 'sum) -> 'a option -> f:('a -> 'sum) -> 'sum
Returns the sum of
f i
for alli
in the container.
val find : 'a option -> f:('a -> bool) -> 'a option
Returns as an
option
the first element for whichf
evaluates to true.
val find_map : 'a option -> f:('a -> 'b option) -> 'b option
Returns the first evaluation of
f
that returnsSome
, and returnsNone
if there is no such element.
val to_list : 'a option -> 'a list
val to_array : 'a option -> 'a array
val min_elt : 'a option -> compare:('a -> 'a -> int) -> 'a option
Returns a minimum (resp maximum) element from the collection using the provided
compare
function, orNone
if the collection is empty. In case of a tie, the first element encountered while traversing the collection is returned. The implementation usesfold
so it has the same complexity asfold
.
include Base.Equal.S1 with type 'a t := 'a option
val equal : 'a Base.Equal.equal -> 'a option Base.Equal.equal
include Base.Invariant.S1 with type 'a t := 'a option
Options form a monad, where return x = Some x
, (None >>= f) = None
, and (Some x
>>= f) = f x
.
include Base.Monad.S with type 'a t := 'a option
module Monad_infix : sig ... end
module Let_syntax : sig ... end
val value_map : 'a option -> default:'b -> f:('a -> 'b) -> 'b
value_map ~default ~f
is the same asfunction Some x -> f x | None -> default
.
val map2 : 'a option -> 'a option -> f:('a -> 'b -> 'c) -> 'a option
map2 o f
maps'a option
and'b option
to a'c option
using~f
.
val value_exn : ?here:Caml.Lexing.position -> ?error:Base.Error.t -> ?message:string -> 'a option -> 'a
value_exn (Some x)
=x
.value_exn None
raises an error whose contents contain the supplied~here
,~error
, andmessage
, or a default message if none are supplied.
val some : 'a -> 'a option
val both : 'a option -> 'a option -> 'a option
val first_some : 'a option -> 'a option -> 'a option
val some_if : bool -> 'a -> 'a option
val merge : 'a option -> 'a option -> f:('a -> 'a -> 'a) -> 'a option
merge a b ~f
merges together the values froma
andb
usingf
. If botha
andb
areNone
, returnsNone
. If only one isSome
, returns that one, and if both areSome
, returnsSome
of the result of applyingf
to the contents ofa
andb
.
val filter : 'a option -> f:('a -> bool) -> 'a option
val try_with : (unit -> 'a) -> 'a option
try_with f
returnsSome x
iff
returnsx
andNone
iff
raises an exception. SeeResult.try_with
if you'd like to know which exception.
val try_with_join : (unit -> 'a option) -> 'a option
try_with_join f
returns the optional value returned byf
if it exits normally, andNone
iff
raises an exception.
val validate : none:unit Base.Validate.check -> some:'a Base.Validate.check -> 'a option Base.Validate.check
include Comparator.Derived with type 'a t := 'a t
val comparator : ('a, 'cmp) Comparator.comparator -> ('a t, 'cmp comparator_witness) Comparator.comparator
include Quickcheckable.S1 with type 'a t := 'a t
val quickcheck_generator : 'a Base_quickcheck.Generator.t -> 'a t Base_quickcheck.Generator.t
val quickcheck_observer : 'a Base_quickcheck.Observer.t -> 'a t Base_quickcheck.Observer.t
val quickcheck_shrinker : 'a Base_quickcheck.Shrinker.t -> 'a t Base_quickcheck.Shrinker.t
module Stable : sig ... end
module Optional_syntax : Optional_syntax.S1 with type 'a t := 'a t and type 'a value := 'a
You might think that it's pointless to have
Optional_syntax
on options because OCaml already has nice syntax for matching on options. The reason to have this here is that you might have, for example, a tuple of an option and some other type that supportsOptional_syntax
. SinceOptional_syntax
can only be opted into at the granularity of the whole match expression, we need thisOptional_syntax
support for options in order to use it for the other half of the tuple.