Skip to content

Instantly share code, notes, and snippets.

@artgon
Created December 22, 2014 21:08
Show Gist options
  • Save artgon/6524874bca5e8dfe1f24 to your computer and use it in GitHub Desktop.
Save artgon/6524874bca5e8dfe1f24 to your computer and use it in GitHub Desktop.
Playing around with channels
(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