Skip to content

Instantly share code, notes, and snippets.

@igrishaev
Created January 25, 2024 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrishaev/c9ab0594cce2e50738c7a1083a320f5a to your computer and use it in GitHub Desktop.
Save igrishaev/c9ab0594cce2e50738c7a1083a320f5a to your computer and use it in GitHub Desktop.
(defn pmap+
"
Like pmap but accepts a custom size of a parallel
window. Not lazy, returns a vector. Takes only
one collection of arguments at the moment.
"
[n func items]
(lazy-seq
(let [[head tail]
(split-at n items)]
(when (seq head)
(let [futures
(for [item head]
(future (func item)))]
(concat
(->> futures
doall
(map deref))
(pmap+ n func tail))))))
#_
(loop [result []
items items]
(let [[head tail] (split-at n items)]
(if (seq head)
(let [futures
(for [item head]
(future (func item)))]
(recur (->> futures
doall
(mapv deref)
(into result))
tail))
result))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment