monadic operator in CCList: map_m_par

This commit is contained in:
Simon Cruanes 2014-07-07 13:41:15 +02:00
parent e7660747d9
commit f3cdb0943e
2 changed files with 14 additions and 0 deletions

View file

@ -526,6 +526,15 @@ module Traverse(M : MONAD) = struct
aux f (x' :: acc) tail aux f (x' :: acc) tail
in aux f [] l in aux f [] l
let rec map_m_par f l = match l with
| [] -> M.return []
| x::tl ->
let x' = f x in
let tl' = map_m_par f tl in
x' >>= fun x' ->
tl' >>= fun tl' ->
M.return (x'::tl')
let sequence_m l = map_m (fun x->x) l let sequence_m l = map_m (fun x->x) l
let rec fold_m f acc l = match l with let rec fold_m f acc l = match l with

View file

@ -236,6 +236,11 @@ module Traverse(M : MONAD) : sig
val fold_m : ('b -> 'a -> 'b M.t) -> 'b -> 'a t -> 'b M.t val fold_m : ('b -> 'a -> 'b M.t) -> 'b -> 'a t -> 'b M.t
val map_m : ('a -> 'b M.t) -> 'a t -> 'b t M.t val map_m : ('a -> 'b M.t) -> 'a t -> 'b t M.t
val map_m_par : ('a -> 'b M.t) -> 'a t -> 'b t M.t
(** Same as {!map_m} but [map_m_par f (x::l)] evaluates [f x] and
[f l] "in parallel" before combining their result (for instance
in Lwt). *)
end end
(** {2 Conversions} *) (** {2 Conversions} *)