Why not one one wheel source? Why nested loops for parts sources?


#1

I am working through the core.async videos and exercises, which are great.

In my first effort to build up the car assembly line, I noticed that I used a different graph structure relating the go blocks (workers) than the one in the solution. Mine seems to work, but I just wanted some feedback if perhaps it works by accident, and there is something unwise about the solution I chose.

The essential difference is that I have one worker who keeps reaching for parts and puts wheels onto a wheel channel, instead of having two separate workers for this. Also, I have only one loop in those blocks. Is there any reason to think twice about this?

(defn my-assembly-line []
  (let [wheel-src                 (chan)
        body-src                  (chan)
        body-with-two-wheels-chan (chan)
        body-with-wheel-chan      (chan)
        boxed-up-body-chan        (chan)]

    (go 
      (loop [part (take-part)]
        (when (body? part)
          (>! body-src part))
        (recur (take-part))))

    (go 
      (loop [part (take-part)]
        (when (wheel? part)
          (>! wheel-src part))
        (recur (take-part))))

    (go 
      (while true
        (let [body  (<! body-src)
              wheel (<! wheel-src)
              body-with-wheel (attach-wheel body wheel)]
          (>! body-with-wheel-chan body-with-wheel))))

    (go
      (while true
        (let [wheel (<! wheel-src)
              body-with-wheel (<! body-with-wheel-chan)
              car (attach-wheel body-with-wheel wheel)]
          (>! body-with-two-wheels-chan car))))
    
    (go 
      (while true
        (let [car (<! body-with-two-wheels-chan)
              car-boxed-up (box-up car)]
          (>! boxed-up-body-chan car-boxed-up))))

    (go
      (time
       (do
       (dotimes [x 10]
         (time
          (do
          (let [car-boxed-up (<! boxed-up-body-chan)]
            (put-in-truck car-boxed-up))))))))))

#2

Hey Alexis,

Thanks for the question.

Actually, there’s no problem with what you did. I was just translating the code more directly. Later we factor out the two wheel go blocks into one.

Eric