List.With_errors
Extended With_errors
, adding functions specific to working with lists in an error-prone context.
include Travesty.Traversable_types.S1_on_monad
with type 'a t := 'a list
and module M := Base.Or_error
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] *)