Skip to content

Commit

Permalink
Improve promote_map implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
n-osborne committed Oct 2, 2024
1 parent 82a2edd commit 1e0631b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion plugins/qcheck-stm/src/reserr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,19 @@ let rec promote = function
let* _ = warns ws and* _ = filter_errs errs in
promote xs

let promote_map f l = List.map f l |> promote
let promote_map f =
let rec aux = function
| [] -> ok []
| x :: xs -> (
match f x with
| (Ok _, _) as x ->
let* y = x and* ys = aux xs in
ok (y :: ys)
| Error errs, ws ->
let* _ = warns ws and* _ = filter_errs errs in
aux xs)
in
aux

let promote_mapi f =
let rec aux i = function
Expand Down
1 change: 1 addition & 0 deletions plugins/qcheck-stm/src/reserr.mli
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ val promote : 'a reserr list -> 'a list reserr
[Warning] in the warnings list *)

val promote_map : ('a -> 'b reserr) -> 'a list -> 'b list reserr
(** [promote_map f xs] is [List.map f xs |> promote] with only one traversal *)

val promote_mapi : (int -> 'a -> 'b reserr) -> 'a list -> 'b list reserr
(** [promote_mapi f xs] is [List.mapi f xs |> promote] with only one traversal *)
Expand Down

0 comments on commit 1e0631b

Please sign in to comment.