Skip to content

Instantly share code, notes, and snippets.

@halgari
Created July 24, 2013 18:57
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 halgari/6073419 to your computer and use it in GitHub Desktop.
Save halgari/6073419 to your computer and use it in GitHub Desktop.
(ns hoare.problem
(:require [clojure.core.async :refer :all]))
(defn fan-in
([ins] (fan-in (chan) ins))
([c ins]
(go (while true
(let [[x] (alts! ins)]
(>! c x))))
c))
(defn service []
(let [in (chan)
out (chan)]
(go (while true
(let [val (<! in)]
(>! out (str val " done")))))
[in out]))
;; On the console I see messages coming from c
;; that have not been modified by the service;
;; furthermore they're coming out of order,
;; such as "b 9" before "b 8 done".
;; Don't know what's GO ing on here.
(defn test-fan-in []
(let [[a-in a-out] (service)
[b-in b-out] (service)
c (fan-in [a-out b-out])]
(go (while true
(println "c:" (<! c))))
(go (dotimes [i 10]
(>! a-in (str "a " i))))
(go (dotimes [i 10]
(>! b-n (str "b " i)))))
nil)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment