Alpha/Endorser: use filter rather than filter_map

This commit is contained in:
Raphaël Proust 2018-06-19 14:37:02 +08:00
parent 52dc0c5858
commit 204a1c9b6a
3 changed files with 29 additions and 6 deletions

View File

@ -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 ()

View File

@ -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

View File

@ -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 ;