package travesty

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

Extend creates Extensions for a Monad.S.

Parameters

module M : Base.Monad.S

Signature

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

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

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

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

val tee_m : 'a -> f:('a -> Base.unit M.t) -> 'a M.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.