Module Stdlib320.Result

Basic functions

val value : ('a, 'e) result -> default:'a -> 'a

Gets the value of result if it is not an error. value r ~default:y is

  • x if r = Ok x
  • y if r = Error e
val default : 'a -> ('a, 'e) result -> 'a

A slightly more useful variant of value, where default y r is equivalent to value r ~default:y

val is_ok : ('a, 'e) result -> bool

is_ok r is

  • true if r = Ok x
  • false if r = Error e
val is_error : ('a, 'e) result -> bool

is_error r is equivalent to not (is_ok e)

Higher-order functions

val bind : ('a, 'e) result -> ('a -> ('b, 'e) result) -> ('b, 'e) result

Monadic bind for results. It "does something" to the value of a result if it is not an error, and passes along the error otherwise. bind r f is

  • f x if r = Ok x
  • Error e if r = Error e
val join : (('a, 'e) result, 'e) result -> ('a, 'e) result

Monadic join for results. It collapses a result whose value is result into a single result. join rr is

  • Ok x if rr = Ok (Ok x)
  • Error e1 if rr = Error e1
  • Error e2 if rr = Ok (Error e2)
val map : ('a -> 'b) -> ('a, 'e) result -> ('b, 'e) result

Mapping function for result values. It applies a function to the value of the result if the result is not an error and passes along the error otherwise. map f r is

  • Ok (f x) if r = Ok x
  • Error e if r = Error e
val map_error : ('e -> 'f) -> ('a, 'e) result -> ('a, 'f) result

Mapping function for result errors. Similar to the previous function but on errors. map f r is

  • Ok x if r = Ok x
  • Error (f e) if r = Error e
val fold : ok:('a -> 'c) -> error:('e -> 'c) -> ('a, 'e) result -> 'c

Folding function for results. Similar to the folding for options. fold ~ok:f ~error:g r is

  • f x if r = Ok x
  • g e if r = Error e

Converting

val to_option : ('a, 'e) result -> 'a option

Converts a result into an option, dropping the error information. to_option r is

  • Some x if r = Ok x
  • None if r = Error e
val to_list : ('a, 'e) result -> 'a list

to_list r is equivalent to Option.to_list (to_option r)