;(async/timeout (* 10 1000)) ;; not sure why this doesn’t work here, would
make it portable to clojureScript I think

Did you forget to use <! on that line?


e
​

On Fri, Jan 2, 2015 at 11:04 AM, Thomas <th.vanderv...@gmail.com> wrote:

> Happy New to all of you!!!
>
> Recently I came across the barber problem (again) and while reading it I
> thought that code.async would be ideal for this kind of problem. Below is
> the code I have come up with. I think it does the trick but I am not
> entirely happy with it, for instance the fact that I use the two atoms as
> counters. I also wonder if there is a better way to time the ten seconds,
> something instead of the atom.
>
> So please critique the code below, any comments, improvements,
> obfuscations etc. are welcome!!
>
> Thomas
>
>
> (ns barber.core
>   (:require [clojure.core.async :as async]))
>
> ;; A barber shop takes customers
> ;; Customer arrive at random intervals, from ten to thirty milliseconds
> ;; The barber shop has three chairs in the waiting room
> ;; The barber shop has one barber and one barber chair
> ;; When the barber's chair is empty, a customer sits in the the chair
> ;;   wakes up the barber, and gets a haircut.
> ;; If the chairs are occupied , all new customer will turn away
> ;; Haircuts takes twenty milliseconds
> ;; After a customer receives a haircut, he gets up and leaves.
> ;;
> ;; Write a program that determines how many haircuts a barber can
> ;;   give in ten seconds.
>
> (def running (atom false))
> (def counter1 (atom 0))
> (def counter2 (atom 0))
>
> (defn customers []
>   (let [c (async/chan (async/dropping-buffer 3))]
>      (async/go
>       (while @running
>         (async/<! (async/timeout (+ 10 (rand-int 21))))
>         (async/>! c (swap! counter1 inc))))
>     c))
>
> (defn barber [c]
>   (async/go
>    (while true
>      (let [r (async/<! c)]
>        (async/<! (async/timeout 20))
>        ;(println r)
>        (swap! counter2 inc)))))
>
> (comment
>   (reset! counter1 0)
>   (reset! counter2 0)
>   (reset! running true)
>   (barber (customers))
>   (Thread/sleep (* 10 1000))
>   ;(async/timeout (* 10 1000)) ;; not sure why this doesn't work here,
> would make it portable to clojureScript I think
>   (reset! running false)
>   (println (str "Served " @counter2 " customers out of " @counter1 "
> possible customers"))
> )
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to