From f3cdb0943eea76d887367f7f7595f36507f7a373 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 7 Jul 2014 13:41:15 +0200 Subject: [PATCH] monadic operator in CCList: map_m_par --- core/CCList.ml | 9 +++++++++ core/CCList.mli | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/core/CCList.ml b/core/CCList.ml index 72d5ff87..2fca2327 100644 --- a/core/CCList.ml +++ b/core/CCList.ml @@ -526,6 +526,15 @@ module Traverse(M : MONAD) = struct aux f (x' :: acc) tail 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 rec fold_m f acc l = match l with diff --git a/core/CCList.mli b/core/CCList.mli index 281a6616..6a72b547 100644 --- a/core/CCList.mli +++ b/core/CCList.mli @@ -236,6 +236,11 @@ module Traverse(M : MONAD) : sig 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_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 (** {2 Conversions} *)