Core_kernel.Option
This module extends Base.Option
with bin_io, quickcheck, and support for ppx_optional.
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 module type of struct include Base.Option end with type 'a t := 'a option
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
Checks whether the provided element is there, using equal
.
fold t ~init ~f
returns f (... f (f (f init e1) e2) e3 ...) en
, where e1..en
are the elements of t
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 of fold
that runs in the Result
monad. If f
returns an Error _
, that value is returned without any additional invocations of f
.
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 of fold
. If f
returns Stop _
the computation ceases and results in that value. If f
returns Continue _
, the fold will proceed. If f
never returns Stop _
, the final result is computed by finish
.
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
Returns true
if and only if there exists an element for which the provided function evaluates to true
. This is a short-circuiting operation.
Returns true
if and only if the provided function evaluates to true
for all elements. This is a short-circuiting operation.
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 all i
in the container.
Returns as an option
the first element for which f
evaluates to true.
Returns the first evaluation of f
that returns Some
, and returns None
if there is no such element.
Returns a minimum (resp maximum) element from the collection using the provided compare
function, or None
if the collection is empty. In case of a tie, the first element encountered while traversing the collection is returned. The implementation uses fold
so it has the same complexity as fold
.
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
t >>= f
returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t
to yield a value v
, and then runs the computation returned by f v
.
module Monad_infix : sig ... end
ignore_m t
is map t ~f:(fun _ -> ())
. ignore_m
used to be called ignore
, but we decided that was a bad name, because it shadowed the widely used Caml.ignore
. Some monads still do let ignore = ignore_m
for historical reasons.
module Let_syntax : sig ... end
These are convenient to have in scope when programming with a monad:
value_map ~default ~f
is the same as function Some x -> f x | None -> default
.
map2 o f
maps 'a option
and 'b option
to a 'c option
using ~f
.
call x f
runs an optional function ~f
on the argument.
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
, and message
, or a default message if none are supplied.
merge a b ~f
merges together the values from a
and b
using f
. If both a
and b
are None
, returns None
. If only one is Some
, returns that one, and if both are Some
, returns Some
of the result of applying f
to the contents of a
and b
.
try_with f
returns Some x
if f
returns x
and None
if f
raises an exception. See Result.try_with
if you'd like to know which exception.
try_with_join f
returns the optional value returned by f
if it exits normally, and None
if f
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 supports Optional_syntax
. Since Optional_syntax
can only be opted into at the granularity of the whole match expression, we need this Optional_syntax
support for options in order to use it for the other half of the tuple.