Module Prelude.Option

Option monad and other useful functions on options.

include module type of struct include Stdlib.Option end
type !'a t = 'a option =
  1. | None
  2. | Some of 'a
val none : 'a option
val value : 'a option -> default:'a -> 'a
val fold : none:'a -> some:('b -> 'a) -> 'b option -> 'a
val iter : ('a -> unit) -> 'a option -> unit
val is_none : 'a option -> bool
val is_some : 'a option -> bool
val equal : ('a -> 'a -> bool) -> 'a option -> 'a option -> bool
val compare : ('a -> 'a -> int) -> 'a option -> 'a option -> int
val to_result : none:'e -> 'a option -> ('a, 'e) Stdlib.result
val to_seq : 'a option -> 'a Stdlib.Seq.t

Options

val return : 'a -> 'a t

(return x) is (Some x).

val some : 'a -> 'a t

some is return.

val get : 'a option -> 'a

(get x) is v if (x = Some v) and raises Invalid_argument otherwise.

val default : 'a -> 'a option -> 'a

(default d) is ( either id d)

Predicates

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.

Option Monad

val bind : 'a option -> ('a -> 'b option) -> 'b option

(bind o f) is (Some (f v)) if (o = Some v) and None if (o = None).

val (>>=) : 'a option -> ('a -> 'b option) -> 'b option

(>>=) is bind.

val map : ('a -> 'b) -> 'a option -> 'b option

(map f o) is (bind o (fun v -> Some (f v))).

val (>>|) : 'a option -> ('a -> 'b) -> 'b option

(>>|) is flip map.

val (>=>) : ('a -> 'b option) -> ('b -> 'c option) -> 'a -> 'c option

(>=>) is monadic composition: ((f >=> g) x = (f x) >>= g)

val join : 'a option option -> 'a option

(join o) is v if (o = Some v) and o otherwise.

val on_none : 'a t -> (unit -> 'a) -> 'a t

(on_none o f) is (Some (f ())) if o is None and o otherwise.

val (>>/) : 'a t -> (unit -> 'a) -> 'a t

(>>/) is on_none

val reduce : 'a t list -> 'a list

reduce 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]

Functionals

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 -> list

Exception Handling

val 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.

Type Conversion

val to_bool : 'a t -> bool

(to_bool o) is false if (o = None) and is true otherwise.

val to_list : 'a option -> 'a list

(to_list o) is [v] if (o = Some v) and is [] otherwise.

val to_exn : exn -> 'a t -> 'a

to_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.

Development Utils

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 ()).

Ops

module Ops : sig ... end

Infix and prefix operators.