package travesty
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=216c920c872cef2d52fa58e3c49826766a2cf6f2233e64937f18c46c0c5c5388
sha512=3b4f76794666aa3fb16c3639479790df3478a79e6f582b3e66b144e57df26a76580499961dd374f4fb6f3bd2dd7506e2725ed1242bada9deb14eb1916cacd18f
doc/travesty.base_exts/Travesty_base_exts/Or_error/index.html
Module Travesty_base_exts.Or_error
Source
Or-error monad extensions.
This module contains various extensions for Base
's Or_error
monad, including monadic traversal over successful values and monad extensions.
Defined to let this module be used directly in chaining operations etc.
Travesty signatures
Monad extensions for Or_error
.
include Travesty.Monad_exts_types.S with type 'a t := 'a t
S
subsumes S_let
.
Haskell-style operators
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
.
compose_m f g
is the Kleisli composition of f
and g
.
Guarded monadic computations
map_when_m ?otherwise condition ~f a
is f a
when condition
is true, and otherwise a
(by default, return
) otherwise.
when_m ?otherwise condition ~f
is f ()
when condition
is true, and otherwise ()
(by default, return
) otherwise.
map_unless_m ?otherwise condition ~f a
is f a
when condition
is false, and otherwise a
(by default, return
) otherwise.
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
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) *)
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 () )
Or_error
is a bi-traversable type, with the right type fixed to Error.t
. (This is backwards from Haskell conventions, but matches the position Error.t
takes in Result
in Base
.
include Travesty.Bi_traversable_types.S1_left
with type 'l t := 'l t
and type right = Base.Error.t
include Travesty.Generic_types.Bi_left
with type 'l t := 'l t
with type right = Base.Error.t
Fixed type of right elements.
include Travesty.Bi_traversable_types.Generic
with type ('l, 'r) t := 'l t
and type 'l left := 'l
and type 'r right := right
We can do non-monadic bi-mapping operations.
include Travesty.Bi_mappable_types.Generic
with type ('l, 'r) t := 'l t
and type 'l left := 'l
and type 'r right := right
map_left c ~f
maps f
over the left type of c
only.
module On
(M : Base.Applicative.S) :
Travesty.Bi_traversable_types.Generic_on_applicative
with type ('l, 'r) t := 'l t
and type 'l left := 'l
and type 'r right := right
and module M := M
On
implements bi-traversal operators for a given applicative functor M
.
module On_monad
(M : Base.Monad.S) :
Travesty.Bi_traversable_types.Generic_on_applicative
with type ('l, 'r) t := 'l t
and type 'l left := 'l
and type 'r right := right
and module M := Travesty.Monad_exts.App(M)
On_monad
implements bi-traversal operators for a given monad M
.
module With_errors :
Travesty.Bi_traversable_types.Generic_on_applicative
with type ('l, 'r) t := 'l t
and type 'l left := 'l
and type 'r right := right
and module M := Base.Or_error
With_errors
specialises On_monad
to the error_monad.
On_ok
is shorthand for Traverse1_left
on this module.
Shortcuts for combining errors
These functions are just shorthand for mapping over a list, then using the various combine_errors
functions in Base.
Prefer using these, where possible, over the analogous functions in T_list.With_errors; these ones correctly merge errors.
combine_map xs ~f
is short for map xs ~f
followed by combine_errors
.