Prelude.OptionOption monad and other useful functions on options.
include module type of struct include Stdlib.Option endval return : 'a -> 'a t(return x) is (Some x).
(default d) is ( either id d)
val something : 'a t -> bool(something r) is true if (r = Some v) for any v and false otherwise.
val nothing : 'a t -> bool(nothing r) is true if (r = None) and false otherwise.
(bind o f) is (Some (f v)) if (o = Some v) and None if (o = None).
(>>=) is bind.
(>>|) is flip map.
(>=>) is monadic composition: ((f >=> g) x = (f x) >>= g)
(on_none o f) is (Some (f ())) if o is None and o otherwise.
val reduce : 'a t list -> 'a listreduce is (filter something $ List.map get), i.e. it throws away None's and pulls the values out of Some's.
Example: (map (catch (fun x -> 2 / x)) [-1;0;1] |> Option.reduce) = [-2; 2]
val foldl : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a(foldl f z o) is (f z x) iff (o = Some x), and otherwise is z.
val foldr : ('a -> 'b -> 'b) -> 'b -> 'a t -> 'b(foldr f z o) is (f x z) iff (o = Some x), and otherwise is z.
val either : ('a -> 'b) -> 'b -> 'a t -> 'b(either some none x) is (some v) if x is (Some v) and none otherwise.
val maybe : ('a -> unit) -> 'a t -> unit(maybe f o) is (f x) if o is (Some x), and () otherwise.
val call : 'a -> ('b -> 'a) t -> 'b -> 'a(call def fopt x) is (f x) if fopt is (Some f) and is def otherwise.
This is nice for optional function parameters:
let f ?error x = match f x with
| exception exn -> Exn.to_string exn |> Option.call () error; []
| list -> listval catch : ?this:exn -> ('a -> 'b) -> 'a -> 'b t(catch ?this f x) is (Some (f x)) unless f raises an exception, in which case it is None.
Any exception will result in None; if you want more precision, specify the exception with ~this -- in this case, any other exception will be re-raised: e.g. (catch ((/) 1) 0) is None while (catch ~this:Not_found ((/) 1) 0) raises Division_by_zero.
val to_bool : 'a t -> bool(to_bool o) is false if (o = None) and is true otherwise.
val to_exn : exn -> 'a t -> 'ato_exn exn (Some x) is x and to_exn exn None raises exn.
val of_result : ('a, 'b) Stdlib.result -> 'a t(of_result r) is (Some x) iff (r = Ok x) and is None otherwise.
val to_string : ('a -> string) -> 'a t -> string(to_string conv o) converts the ('a option) o to a string using the conversion function conv.
Examples:
(to_string string_of_int None) = "None"(to_string string_of_int (Some 12)) = "Some 12"val print : ('a -> string) -> 'a t -> unit(print conv) is (to_string conv >> print_endline).
val random : (unit -> 'a) -> unit -> 'a t(random ()) is, with equal probability, either None or (Some v) s.t. (v = r ()).
module Ops : sig ... endInfix and prefix operators.