From 0aa8af7993ab5c9a39af4ea72876ebd264a3cab5 Mon Sep 17 00:00:00 2001 From: Lee Spector Date: Wed, 30 Aug 2023 12:02:46 -0400 Subject: [PATCH] Add pmapallv utility function --- src/propeller/utils.cljc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/propeller/utils.cljc b/src/propeller/utils.cljc index 5035f02..aa0d7ba 100755 --- a/src/propeller/utils.cljc +++ b/src/propeller/utils.cljc @@ -85,3 +85,22 @@ zip/path count (max height)))))) + +(defn pmapallv + "A utility for concurrent execution of a function on items in a collection. +In single-thread-mode this acts like mapv. Otherwise it acts like pmap but: +1) coll should be finite, 2) the returned sequence will not be lazy, and will +in fact be a vector, 3) calls to f may occur in any order, to maximize +multicore processor utilization, and 4) takes only one coll so far." + [f coll args] + #?(:clj (vec (if (:single-thread-mode args) + (doall (map f coll)) + (let [agents (map #(agent % :error-handler + (fn [agnt except] + (clojure.repl/pst except 1000) + (System/exit 0))) + coll)] + (dorun (map #(send % f) agents)) + (apply await agents) + (doall (map deref agents))))) + :cljs (mapv f coll)))