Created
July 14, 2016 19:49
-
-
Save seancorfield/e7fa885e35df232dbb3758735088ae6e to your computer and use it in GitHub Desktop.
pmap using core.async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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