Clojure
Let’s take another look at the assembly line diagram now.
If you pay close attention at each of the workers and observe how they work, you will probably find the 3 workers at the beginning of the assembly line to be a bit … not that smart. All of them only one type of part, either wheels or bodies. If the body worker takes a wheel, he will just abandon it and keep taking new parts until he gets a body.
We need a way to stop the whole assembly line after 10 toy cars have been put in the truck.
But before that, we need to understand more about chan.
Channels are created open Close a channel with close! You can call close! to close a channel. Once a channel is closed, it can never be reopened. Put (>!) on a closed channel returns false. Get (<!) from a closed channel returns nil.
Let’s take the assembly-line function and refactor it to make things a bit more clear, and also as a recap.
(defn go-body [body-chan] (go (while true (let [body (loop [] (let [part (take-part)] (if (body? part) part (recur))))] (prn "Got body") (>! body-chan body)))) ) (defn go-wheel1 [wheel1-chan] (go (while true (let [wheel1 (loop [] (let [part (take-part)] (if (wheel? part) part (recur))))] (prn "Got first wheel") (>! wheel1-chan wheel1))))) (defn go-wheel2 [wheel2-chan] (go (while true (let [wheel2 (loop [] (let [part (take-part)] (if (wheel?
One way to boost productivity in traditional factories is to use assembly line.
From Wikipedia:
An assembly line is a manufacturing process (often called a progressive assembly) in which parts (usually interchangeable parts) are added as the semi-finished assembly moves from workstation to workstation where the parts are added in sequence until the final assembly is produced. By mechanically moving the parts to the assembly work and moving the semi-finished assembly from work station to work station, a finished product can be assembled faster and with less labor than by having workers carry parts to a stationary piece for assembly.
In Part 1 we have already defined the process of building toy cars, by one worker. Here is the code again for easy reference.
(defn build-car [n] (prn n "Starting build") (let [body (loop [] (let [part (take-part)] (if (body? part) part (recur)))) _ (prn n "Got body") wheel1 (loop [] (let [part (take-part)] (if (wheel? part) part (recur)))) _ (prn n "Got wheel1") wheel2 (loop [] (let [part (take-part)] (if (wheel?