It forces the go block to wait until (walker (:left t)) pushes something --
that is, for the recursively-entered go block to complete. Similarly the
right branch is waited for before the parent go block completes. So forcing
an in-order traversal of the tree.


On Fri, Sep 13, 2013 at 5:13 PM, larry google groups <
lawrencecloj...@gmail.com> wrote:

>
> 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