To do that, I create two functions, one that increases the queue of customers (with the shop supporting up to 3 customers) amd one that dispatces customers, decreasing the queue. I assume 4 hours for customers to be able to take a seat (the barber's will close after 4 hours and cut the remaining wating customers).
Notice here that the barber cuts the hair only if a random number meets a condition (here making him slower than his customers). I implement this with a watcher function, that gets called whenever the reference changes. In order to be called every time a customer enter the shop, we need to issue and identity change, that does not change the value of the queue but fires the watcher. This is a very nice feature of Clojure.
The important functional element is this
(let [f (if (< @queue 3) inc identity)]Notice the conditional assignment to the variable in the let block. This removes the boilerplate code needed in the expression section within the let block.
(dosync (alter queue f)) ))
No comments:
Post a Comment