Your code is simple and easy to understand but it would look to see if
atoms can be removed. Also the use go/while loop makes your code not stop
and hence atom like @running is required.

This is my attempt to solve the problem. I started with your code as the
base and refactored it.

Following are the things which differ from your code:

- I have three channels customer-channel, waiting-room-channel,
barber-channel.
- Instead of keeping a @running atom, I made other functions like
waiting-room and barber to stop processing when the channel they are
dependent on is closed.
- Each channel maintain and return the customers they processed. So in the
end the barber channel returns all the customers it processed.
- I had the use mult channels for the customer channel since you are also
printing the total number of customers.

(require '[clojure.core.async :as async])

(do
  (defn customers []
    (let [c (async/chan)]
      (async/go-loop [i 1]
        (async/<! (async/timeout (+ 10 (rand-int 21))))
        (when (async/>! c (str "Customer" i))
          (recur (inc i))))
      c))

  (defn waiting-room [customer-channel]
    (let [waiting-channel (async/chan (async/dropping-buffer 3))]
      (async/pipe customer-channel waiting-channel)
      waiting-channel))

  (defn barber [waiting-channel]
    (async/go-loop [processed-customers []]
      (if-let [customer (async/<! waiting-channel)]
        (do
          (async/<! (async/timeout 100))
          (recur (conj processed-customers customer)))
        processed-customers)))

  (let [customers-channel (customers)
        customers-mult-channel (async/mult customers-channel)
        all-customers-channel (async/tap customers-mult-channel
(async/chan))
        saloon-customers-channel (async/tap customers-mult-channel
(async/chan))
        saloon (-> saloon-customers-channel
                   waiting-room
                   barber)
        customer-counter (async/reduce (fn [i c] (inc i)) 0
all-customers-channel)]
    (async/<!! (async/timeout 1000))
    (async/close! customers-channel)
    (println "Haircuts for " (count (async/<!! saloon)) " customers out of
" (async/<!! customer-counter) " customers")))

On Sun, Jan 4, 2015 at 9:27 PM, Russell <russ...@russelldunphy.com> wrote:

> Looks like the barber's loop doesn't check the "running" atom, so it will
> never stop...
>
> If you wanted to get rid of the atom for counting haircuts you could use a
> go-loop with a haircut count as an argument. Something like:
>
> (go-loop [haircuts 0]
>   (if @running
>     (do (cut-hair!)
>         (recur (inc haircuts)))
>     haircuts))
>
> Other than that, I'd find it easier to understand what the program was
> doing if you extracted out some of the code into functions named in the
> language of the domain. Even if all the cut-hair! function contains is (<!!
> (async/timeout 20)) it makes it easier to understand what that timeout
> *means*. Similarly, you could probably come up with a name for the
> channel passed to the barber function that's a bit more descriptive than
> "c" :-).
>
> --
> 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