On 17 May 2017, at 19:14, Kevin Kleinfelter <kleinfelter.gro...@gmail.com> 
wrote:

> I'm stumped by the behavior of the following code fragment.  Can someone help 
> me understand what's happening?
> 
> This code:
>   (println "Holding:" (:class holding))
>   (let [t (:class holding)]
>       (for [x t] (println "here" x))
>       (for [x t] (println "there" x))))
> 
> Produces this output:
> holding: {:fundname Mutual Fund 1, :value 123.45, :class [{:class sell-me, 
> :percent 100}]}
> class: [{:class sell-me, :percent 100}]
> Holding: [{:class sell-me, :percent 100}]
> there {:class sell-me, :percent 100}
> 
> Why doesn't the 'for' with "here" print anything?

for produces a lazy seq so it does nothing unless you call it. 

The value of a let in Clojure with more than one subexpression after the 
binding is the value of the value of the last subexpression. So in your let, 
the second for is evaluated because it is called to provide the return value of 
the let. The first expression isn't evaluated because it isn’t part of the 
return value so it isn’t called.

> I tried wrapping the fors with a do, but the output was the same:
>   (println "class:" (:class holding))
>   (let [t (:class holding)]
>     (do
>         (for [x t] (println "here" x))
>         (for [x t] (println "there" x)))))

The documentation for do 

https://clojuredocs.org/clojure.core/do

explains that do evaluates the expressions in order and returns the last. Your 
last expression is evaluated because it is returned. The first expression is 
not returned, so it isn’t being used and isn’t evaluated.

One way of evaluating both would be to write:

(let [t (:class holding)]
        (doseq [a (for [x t] (println "here" x))
                     b (for [x t] (println "there" x))]
              [a b]))

https://clojuredocs.org/clojure.core/doseq

This has a return value of nil and prints both “here” and “there”.

You could also just use a let:

(let [t (:class holding)
       a (for [x t] (println "here" x))
       b (for [x t] (println "there" x))]
              [a b])

This prints both “here” and “there” and has a return value of a vector with two 
seqs of nils.

If you don’t want the return value, the doseq is the right way to go. If you do 
want a return value use a let.

Alan

-- 
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/d/optout.

Reply via email to