diff --git a/src/lib_error_monad/error_monad.ml b/src/lib_error_monad/error_monad.ml index fedd33e6a..c43ada003 100644 --- a/src/lib_error_monad/error_monad.ml +++ b/src/lib_error_monad/error_monad.ml @@ -438,6 +438,28 @@ module Make(Prefix : sig val id : string end) = struct tt >>=? fun rt -> return (rh :: rt) + let rec filter_s f l = + match l with + | [] -> return [] + | h :: t -> + f h >>=? function + | false -> filter_s f t + | true -> + filter_s f t >>=? fun t -> + return (h :: t) + + let rec filter_p f l = + match l with + | [] -> return [] + | h :: t -> + let jh = f h + and t = filter_p f t in + jh >>=? function + | false -> t + | true -> + t >>=? fun t -> + return (h :: t) + let rec iter_s f l = match l with | [] -> return () diff --git a/src/lib_error_monad/error_monad_sig.ml b/src/lib_error_monad/error_monad_sig.ml index 99a85e14a..bad2f0f30 100644 --- a/src/lib_error_monad/error_monad_sig.ml +++ b/src/lib_error_monad/error_monad_sig.ml @@ -151,6 +151,12 @@ module type S = sig val filter_map_p : ('a -> 'b option tzresult Lwt.t) -> 'a list -> 'b list tzresult Lwt.t + (** A {!List.filter} in the monad *) + val filter_s : + ('a -> bool tzresult Lwt.t) -> 'a list -> 'a list tzresult Lwt.t + val filter_p : + ('a -> bool tzresult Lwt.t) -> 'a list -> 'a list tzresult Lwt.t + (** A {!List.fold_left} in the monad *) val fold_left_s : ('a -> 'b -> 'a tzresult Lwt.t) -> 'a -> 'b list -> 'a tzresult Lwt.t diff --git a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml index a295bcd34..2eb1bcbfc 100644 --- a/src/proto_alpha/lib_delegate/client_baking_endorsement.ml +++ b/src/proto_alpha/lib_delegate/client_baking_endorsement.ml @@ -144,12 +144,7 @@ let prepare_endorsement (cctxt : #Proto_alpha.full) ~(max_past:int64) state bi = let time = Time.(add (now ()) state.delay) in let timeout = Lwt_unix.sleep (Int64.to_float state.delay) in tzforce state.delegates >>=? fun delegates -> - filter_map_p - (fun delegate -> - allowed_to_endorse cctxt bi delegate >>=? function - | true -> return (Some delegate) - | false -> return None) - delegates >>=? fun delegates -> + filter_p (allowed_to_endorse cctxt bi) delegates >>=? fun delegates -> state.pending <- Some { time ; timeout ;