Skip to content

Instantly share code, notes, and snippets.

@seancorfield
Created July 14, 2016 19:49
Show Gist options
  • Save seancorfield/e7fa885e35df232dbb3758735088ae6e to your computer and use it in GitHub Desktop.
Save seancorfield/e7fa885e35df232dbb3758735088ae6e to your computer and use it in GitHub Desktop.
pmap using core.async
(ns async-example.core
(:require [clojure.core.async :refer [chan go >! <!!]])
(:gen-class))
(defn my-pmap [f col]
(let [chans (repeatedly (count col) chan)]
(doseq [[c e] (map vector chans col)]
(go (>! c (f e))))
(map <!! chans)))
(defn -main [& args]
(println (my-pmap (fn [x] (Thread/sleep 1000) (identity x)) (range 0 10))))
time lein run
(0 1 2 3 4 5 6 7 8 9)
lein run 3.33s user 0.26s system 71% cpu 5.046 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment