Module Stdlib320.Result

A module containing basic result operations

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)

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, i.e. map f r is

  • Ok x if r = Ok x
  • Error (f e) if r = Error e
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