Lazy evaluation strikes again! map doesn't actually do anything until
you force it eg with dorun. This is quite a common mistake! Does
anyone know how one would go able making a warning be printed when a
lazy seq is created and thrown away without any evaluation? I'd be
interested in implementing it with some pointers on where to start
(I've read quite a lot of the code-base but well I'm not a language
expert).

To fix your problem:

  (let [b (new StringBuffer)]
    (dorun (map #(.append b (str % " ")) ["blah" "blah" "blah"]))
    b)

I think it preferable to use doseq for side-effects:
  (let [b (new StringBuffer)]
    (doseq [s ["blah" "blah" "blah"]] (.append b (str s " "))
    b)


Regards,
Tim.


On May 31, 10:47 am, Ben <ben.he...@gmail.com> wrote:
> I'm not sure if this is an issue or not.  I broke it down to a simple
> case.  When I pass a Java object into a map closure the object doesn't
> seem to get updated:
>
> (let [b (new StringBuffer)]
>   (map #(.. b (append (str % " "))) '("blah" "blah" "blah")) b) ;=>
> #<StringBuffer >
>
> expected ;=> #<StringBuffer blah blah blah >
>
> I know it's doing working within map because if I take the ending b
> off I get
>
> (let [b (new StringBuffer)]
>   (map #(.. b (append (str % " "))) '("blah" "blah" "blah")))
>
> ;=> (#<StringBuffer blah blah > #<StringBuffer blah blah blah >
> #<StringBuffer blah blah blah >)
>
> any ideas?  Is this a bug or is this how it's supposed to work?  I
> realize there are better ways to do this, but I wanted to create a
> simplified example of what I was noticing.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to