package travesty

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

Module Monad_exts.ExtendSource

Extend creates extensions for a Monad.S.

Parameters

module M : Base.Monad.S

Signature

S subsumes S_let.

include Monad_exts_types.S_let with type 'a t := 'a M.t
Sourceval (let+) : 'a M.t -> ('a -> 'b) -> 'b M.t

let+ is map.

Sourceval (let*) : 'a M.t -> ('a -> 'b M.t) -> 'b M.t

let* is bind.

Haskell-style operators

Sourceval then_m : _ M.t -> 'a M.t -> 'a M.t

then_m x y sequentially composes the actions x and y as with >>=, but, rather than using the returned value of x, it instead just returns y.

Sourceval (>>) : _ M.t -> 'a M.t -> 'a M.t

x >> y is then_m x y.

Sourceval compose_m : ('a -> 'b M.t) -> ('b -> 'c M.t) -> 'a -> 'c M.t

compose_m f g is the Kleisli composition of f and g.

Sourceval (>=>) : ('a -> 'b M.t) -> ('b -> 'c M.t) -> 'a -> 'c M.t

x >=> y is compose_m x y.

Guarded monadic computations

Sourceval map_when_m : ?otherwise:('a -> 'a M.t) -> bool -> 'a -> f:('a -> 'a M.t) -> 'a M.t

map_when_m ?otherwise condition ~f a is f a when condition is true, and otherwise a (by default, return) otherwise.

Sourceval when_m : ?otherwise:(unit -> unit M.t) -> bool -> f:(unit -> unit M.t) -> unit M.t

when_m ?otherwise condition ~f is f () when condition is true, and otherwise () (by default, return) otherwise.

Sourceval map_unless_m : ?otherwise:('a -> 'a M.t) -> bool -> 'a -> f:('a -> 'a M.t) -> 'a M.t

map_unless_m ?otherwise condition ~f a is f a when condition is false, and otherwise a (by default, return) otherwise.

Sourceval unless_m : ?otherwise:(unit -> unit M.t) -> bool -> f:(unit -> unit M.t) -> unit M.t

unless_m ?otherwise condition ~f is f () when condition is false, and otherwise () (by default, return) otherwise.

Executing monadic effects in the middle of pipelines

Sourceval tee_m : 'a -> f:('a -> unit M.t) -> 'a M.t

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

Example (using an extended Or_error):

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

  (* Ok (1764) *)
Sourceval tee : 'a -> f:('a -> unit) -> 'a M.t

tee val ~f behaves as tee_m, but takes a non-monadic f.

Example (using an extended Or_error):

  let print_if_negative x =
    if Int.negative x then Stdio.print_string "value is negative!"
  in
  Or_error.(
    try_get_value () >>= tee ~f:print_if_negative >>= try_use_value ())
OCaml

Innovation. Community. Security.