Created
December 29, 2014 08:55
-
-
Save slipset/fb7d2a44303481731d89 to your computer and use it in GitHub Desktop.
Implementation of the barbershop problem with 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 barbershop.core | |
(:require [clojure.core.async :refer | |
[dropping-buffer timeout chan go-loop <! chan ]])) | |
(def shop (chan (dropping-buffer 3))) | |
(defn customer [i shop] | |
(put! shop i (fn [v] (println "customer" i "going to the barber")))) | |
(defn barber [shop] | |
(go-loop [] | |
(let [v (<! shop)] | |
(println "cutting hair of customer " v) | |
(<! (timeout (+ 100 (rand-int 600))))) | |
(recur))) | |
(barber shop) | |
(doseq [n (range 1 200)] | |
(Thread/sleep (+ 100 (rand-int 200))) | |
(customer n shop)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment