package travesty

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Extensions contains extensions for a Monad.S.

To create an instance of Extensions, use Extend.

type 'a t

The type of the extended monad.

val when_m : Base.bool -> f:(Base.unit -> Base.unit t) -> Base.unit t

when_m predicate ~f returns f () when predicate is true, and return () otherwise.

val unless_m : Base.bool -> f:(Base.unit -> Base.unit t) -> Base.unit t

unless_m predicate ~f returns f () when predicate is false, and return () otherwise.

val tee_m : 'a -> f:('a -> Base.unit t) -> 'a t

tee_m val ~f executes f val for its monadic action, then returns val.

Example:

let fail_if_negative x =
  T_on_error.when_m (Int.is_negative x)
    ~f:(fun () -> Or_error.error_string "value is negative!")
in
Or_error.(
  42 |> T_on_error.tee_m ~f:fail_if_negative >>| (fun x -> x * x)
) (* Ok (1764) *)
OCaml

Innovation. Community. Security.