The go block itself (and thus any call to walker) returns a channel. This
value is indeed thrown away; its utility lies entirely in the fact that the
caller had to wait for it. All of the actual values from the tree are
written to the supplied channel 'ch', and the ultimate caller of this
function will have to take them from there.

I am stupid and recursion is clearly beyond my intellect. Martin Trojer has
a great blog post here which I learned a lot from but I don't understand
why the final example works:

http://martintrojer.github.io/clojure/2013/07/17/non-tailrecursive-functions-in-coreasync/

He offers this as an example of recursively walking a binary search tree:

(defn walk [tree ch]
  (letfn [(walker [t]
            (go
             (when t
               (walker (:left t))
               (>! ch (:value t))
               (walker (:right t)))))]
    (go
     (<! (walker tree))
     (close! ch))))

and then he writes:

"This looks promising, but the results in the channel can be in any
order (since there are no order guarantees in the scheduling of go
processes) – this also means that some of the values might be missing
since the “top” go process can be scheduled before a child one. We
need a little bit more synchronisation to arrive at a working
solution."


and then offers this as the final working example:

(defn walk [tree ch]
  (letfn [(walker [t]
            (go
             (when t
               (<! (walker (:left t)))
               (>! ch (:value t))
               (<! (walker (:right t))))))]
    (go
     (<! (walker tree))
     (close! ch))))

I am confused what this line does:

               (<! (walker (:left t)))

It looks like we are pulling a value off channel that's returned from
that call to walker? But why? We seem to be throwing this value away?
I don't see it being stored anywhere.

How does this give us synchronization?

 --
-- 
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/groups/opt_out.

-- 
-- 
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/groups/opt_out.

Reply via email to