Created
December 22, 2014 21:08
-
-
Save artgon/6524874bca5e8dfe1f24 to your computer and use it in GitHub Desktop.
Playing around with channels
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 channels.core | |
(:require [clojure.core.async :as async :refer [chan go >! <!!]]) | |
(:gen-class)) | |
(def sum (atom 0)) | |
(defn summing [start end loops] | |
(let [c (chan loops) | |
piece (/ end loops)] | |
(dotimes [i loops] | |
(go (>! c (reduce + (range (* piece i) (* piece (inc i))))))) | |
(dotimes [i loops] | |
(let [data (<!! c)] | |
(dosync (swap! sum + data)))))) | |
(defn -main [end loops] | |
(reset! sum 0) | |
(time (summing 0 (read-string end) (read-string loops))) | |
(println @sum)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment