On 20 July 2010 11:50, Paul Richards <paul.richa...@gmail.com> wrote: > So back to my example: > > (def forty-two 42) > > (defn func [] (* forty-two forty-two)) > > (defn other-func [] (binding [forty-two 6] (func))) > > > "func" is impure, and "other-func" is pure. It's really nothing to do > with whether the "binding" keyword is used in the function... I think > I took the sentence from the book too literally.
Technically, a function that depends on impure functions cannot ever be pure. I other-func's case it seems obvious what should happen: func depends on forty-two, and other-func supplies a fixed value for forty-two, so other-func will always return the same result, right? Actually, there this assumption can bite you in several instances: 1. If func were to execute in another thread for some reason, it would see forty-two as 42 because (binding) doesn't cross thread boundaries. 2. If func were memoized, some other part of the code could already have called func without rebinding forty-two, or rebinding it to completely different value. This would make func always yield the cached value, completely ignoring any (binding) forms. 3. If func returned a lazy-seq of values, e.g. by using standard seq functions like map, any calculations not immediately realized in (binding)'s dynamic scope will see 42, not 6. Re-binding Vars is a tricky topic. I haven't written a lot of non-trivial Clojure code so far, but I've yet to come across a use case for (binding) which doesn't suffer from these issues :-) -- Daniel -- 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