module Option: Optiontype'at ='a option
let data = [(2, "two"); (5, "five"); (8, "eight")];;
let f x = match List.Assoc.find_opt x data with
| Some y -> y
| None -> "zero" (* where "zero" is some default value *);;
rather than
let f x = try List.Assoc.find x data with Not_found -> "zero";;
In this case using an exception is shorter, but in nontrivial code options
are easier to understand.include Container.S1
include Monad.S
return x = Some x,
(None >>= f) = None, and (Some x >>= f) = f x.val is_none : 'a t -> boolis_none t returns true iff t = None.val is_some : 'a t -> boolis_some t returns true iff t = Some x.val value_map : 'a t -> default:'b -> f:('a -> 'b) -> 'bvalue_map t ~f ~default is equivalent to value (map t ~f) ~default, except that
it is slightly faster since it avoids creating the intermediate option. I.e.
value_map None ~default ~f = default
value_map (Some x) ~default ~f = f x
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c tmap2 o f map 'a option and 'b option to a 'c option using ~fval call : 'a -> f:('a -> unit) t -> unitcall x f run optional function on argumentval apply : 'a -> f:('a -> 'b) t -> 'b tapply x f run optional function on argument and return an optionval value : 'a t -> default:'a -> 'avalue None ~default = default
value (Some x) ~default = xval value_exn : ?here:Source_code_position0.t ->
?error:Error.t -> ?message:string -> 'a t -> 'avalue_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.val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> boolval some : 'a -> 'a tval both : 'a t -> 'b t -> ('a * 'b) tval first_some : 'a t -> 'a t -> 'a tval some_if : bool -> 'a -> 'a tval filter : f:('a -> bool) -> 'a t -> 'a tval try_with : (unit -> 'a) -> 'a ttry_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.val compare : cmp:('a -> 'a -> int) -> 'a t -> 'a t -> intval validate : none:unit Validate.check ->
some:'a Validate.check -> 'a t Validate.checkval t_of_sexp : (Sexplib.Sexp.t -> 'a) -> Sexplib.Sexp.t -> 'a tval sexp_of_t : ('a -> Sexplib.Sexp.t) -> 'a t -> Sexplib.Sexp.tval bin_t : 'a Bin_prot.Type_class.t -> 'a t Bin_prot.Type_class.tval bin_read_t : 'a Bin_prot.Unsafe_read_c.reader -> 'a t Bin_prot.Read_ml.readerval bin_read_t_ : 'a Bin_prot.Unsafe_read_c.reader -> 'a t Bin_prot.Unsafe_read_c.readerval bin_read_t__ : 'a Bin_prot.Unsafe_read_c.reader ->
(int -> 'a t) Bin_prot.Unsafe_read_c.readerval bin_reader_t : 'a Bin_prot.Type_class.reader -> 'a t Bin_prot.Type_class.readerval bin_size_t : 'a Bin_prot.Size.sizer -> 'a t Bin_prot.Size.sizerval bin_write_t : 'a Bin_prot.Unsafe_write_c.writer -> 'a t Bin_prot.Write_ml.writerval bin_write_t_ : 'a Bin_prot.Unsafe_write_c.writer ->
'a t Bin_prot.Unsafe_write_c.writerval bin_writer_t : 'a Bin_prot.Type_class.writer -> 'a t Bin_prot.Type_class.writerreturn x = Some x,
(None >>= f) = None, and (Some x >>= f) = f x.is_none t returns true iff t = None.is_some t returns true iff t = Some x.value_map t ~f ~default is equivalent to value (map t ~f) ~default, except that
it is slightly faster since it avoids creating the intermediate option. I.e.
value_map None ~default ~f = default
value_map (Some x) ~default ~f = f x
map2 o f map 'a option and 'b option to a 'c option using ~f
call x f run optional function on argument
apply x f run optional function on argument and return an option
value None ~default = default
value (Some x) ~default = x
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.
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.