The problem is that map returns a lazy seq, and the lazy seq is
evaluated outside of the binding by the REPL.  If you add a doall
inside the binding, it behaves as you expect.

user=> (binding [*v* 2] (doall (map f [1 1 1])))
(3 3 3)

Sean

On Feb 8, 5:47 am, Alex <alexander.bolodu...@gmail.com> wrote:
> Hi,
>
> I have a question about the scope of "binding" of a var.
>
> Let's say I have the following var:
>
>     (def *v* 1)
>
> And I define a function that uses it:
>
>     (defn f [n] (+ *v* n))
>
> "binding" behaves as expected, establishing a thread-local binding to
> a new value in its scope:
>
>     user=> (binding [*v* 2] (f 1))
>     3
>
> But if I pass it to "map", I expected it to pick up the new value, but
> it does not.
>
>     user=> (binding [*v* 2] (map f [1 1 1]))
>     (2 2 2)
>
> The output should be "(3 3 3)" if I understand it correctly. Where am I wrong?

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