Module Stdlib320.Option

Basic functions

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

Gets the value of an option if it is not None. value o ~default:y is

  • x if o = Some x
  • y if o = None
val default : 'a -> 'a option -> 'a

A slightly more convenient variant of the previous function, where default y o is equivalent to value o ~default:y.

val is_none : 'a option -> bool

is_none o is

  • true if o = Some x
  • false if o = None
val is_some : 'a option -> bool

is_some o is equivalent to not (is_none o)

Higher-order functions

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

Monadic bind for options. It "does something" to the value of the option if it is not None, and passes along the None otherwise. bind o f is

  • f x if o = Some x
  • None if o = None
val join : 'a option option -> 'a option

Monadic join for options. If collapses an option whose value is an option into a single option. join oo is

  • Some x if oo = Some (Some x)
  • None if oo = Some None or oo = None
val map : ('a -> 'b) -> 'a option -> 'b option

Mapping function for options. It applies a function to the value of the option if the option is not None. map f o is

  • Some (f x) if o = Some x
  • None if o = None
val fold : none:'a -> some:('b -> 'a) -> 'b option -> 'a

Folding function for options. fold ~none:x some:f o is equivalent to default x (map f o).

Converting

val to_result : none:'e -> 'a option -> ('a, 'e) Stdlib.result

Converts an option to a result. to_result ~none:e o is

  • Ok x if o = Some x
  • Error e if o = None
val to_list : 'a option -> 'a list

Converts an option into a list with zero or one element. It is equivalent to

fold ~none:[] ~some:(fun x -> x :: [])