doseq is the idiomatic way to write imperative code segments like this (add-watch generates a side-effect). Nevertheless, I too love using map for stuff like that. All you need to do is surround the map with dorun (or doall if you need the return value) and it will force the computation.
see: (doc dorun) and (doc doall) On Mar 2, 7:06 am, max3000 <maxime.lar...@gmail.com> wrote: > Hi, > > I find the laziness in clojure very hard to wrap my head around. I > understand the idea and it's probably nice in theory. However, in real > life it doesn't seem really useful beyond hardcore mathematical > problems. > > Case in point, I just spent 2 hours debugging a piece of code (shown > below) that seemed simple enough. This is the 3rd time this week that > I've lost substantial time to laziness. I'm pretty pissed to tell the > truth and I find myself wrapping things in doseq more and more just to > be sure. I rarely use 'for' anymore, what's the point? > > Here is the code that gave me trouble: > > (map #(add-watch % watcher callback-fn) all-agents) > > This was not executing. I had to change it to the below expression: > > (doseq [agent all-labor-agents] > (add-watch agent total-labor-agent callback-fn)) > > This second expression seems less elegant than the map above. Why > doesn't clojure realize that an add-watch really should actually loop > over all-agents? Why is it that Java calls are not made in similar > expressions? > > Is laziness so useful that we should waste time investigating and > fixing errors like this? Sure, there could be special constructs for > laziness when we really need it. However, clojure shouldn't default to > it IMO. At this point, laziness is a nice concept but it feels > somewhat removed from reality to tell the truth. Of course I want to > iterate over my collection when I'm doing an add-watch! > > What am I missing? > > Thanks, > > Max --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---