package travesty

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

Extended With_errors, adding functions specific to working with lists in an error-prone context.

include Travesty.Traversable.S1_on_monad with type 'a t := 'a list and module M := Base.Or_error
include Travesty.Traversable.Generic_on_monad with type 'a t := 'a list and type 'a elt := 'a with module M := Base.Or_error
include Travesty.Traversable.Basic_generic_on_monad with type 'a t := 'a list with type 'a elt := 'a with module M := Base.Or_error

Generic refers to the container type as 'a t, and the element type as 'a elt; substitute t/elt (arity-0) or 'a t/'a (arity-1) accordingly below.

include Travesty.Types_intf.Generic with type 'a t := 'a list with type 'a elt := 'a
val map_m : 'a list -> f:('a -> 'b Base.Or_error.t) -> 'b list Base.Or_error.t

map_m c ~f maps f over every t in c, threading through monadic state.

Example:

(* Travesty_base_exts.List adds monadic traversals to a list;
   With_errors (in S1_container) implements them on the On_error
   monad. *)

let f x =
  Or_error.(if 0 < x then error_string "negative!" else ok x)
in
List.With_errors.map_m integers ~f
val fold_map_m : 'a list -> f:('acc -> 'a -> ('acc * 'b) Base.Or_error.t) -> init:'acc -> ('acc * 'b list) Base.Or_error.t

fold_map_m c ~f ~init folds f monadically over every t in c, threading through an accumulator with initial value init.

val fold_m : 'a list -> init:'acc -> f:('acc -> 'a -> 'acc Base.Or_error.t) -> 'acc Base.Or_error.t

fold_m x ~init ~f folds the monadic computation f over x, starting with initial value init, and returning the final value inside the monadic effect.

val iter_m : 'a list -> f:('a -> Base.unit Base.Or_error.t) -> Base.unit Base.Or_error.t

iter_m x ~f iterates the monadic computation f over x, returning the final monadic effect.

val mapi_m : f:(Base.int -> 'a -> 'b Base.Or_error.t) -> 'a list -> 'b list Base.Or_error.t

mapi_m ~f x behaves as mapM, but also supplies f with the index of the element. This index should match the actual position of the element in the container x.

val sequence_m : 'a Base.Or_error.t list -> 'a list Base.Or_error.t

sequence_m x lifts a container of monads x to a monad containing a container, by sequencing the monadic effects from left to right.

Utility functions for modifying lists with error-prone functions

val replace_m : 'a list -> int -> f:('a -> 'a option Or_error.t) -> 'a list Or_error.t

replace_m xs at ~f tries to replace the value at index at in xs using the possibly-failing function f. f may return Ok None, in which case the item is removed

Examples:

replace [1; 2; 3] 1 ~f:(fun _ -> Ok None) (* Ok [1; 3] *)
replace [1; 2; 3] 2 ~f:(fun x -> Ok (Some (x + 1))) (* Ok [1; 2; 4] *)
OCaml

Innovation. Community. Security.