Brian Craft <[email protected]> writes:
Hi Brian,
> I just came across this example in "Clojure Programming", chapt 4:
>
> (def ^:dynamic *max-value* 255)
> (defn valid-value? [v] (<= v *max-value*))
>
> (binding [*max-value* 500] (map valid-value? [299]))
> ;= (false)
>
> It's not really explained in the text. I'm guessing this happens because
> when the (binding...) expression is evaluated, it merely returns the head
> of the seq. The values of the seq aren't evaluated until the repl
> implicitly realizes the return value.
Right.
> The workaround in the book is to move the "binding" into the map
> function, though I would think "doall" would be a better solution.
>
> (binding [*max-value* 500] (doall (map valid-value? [299])))
> ;= (true)
>
> This seems like the same issue that comes up with libraries that
> implement a with-<blah> API: with-db, and so-forth. You have to walk
> any seqs within the binding form if the binding is to have any affect
> on the values in the seqs.
Another way is to use `bound-fn' which has the benefit that you don't
need to realize the complete lazy seq in advance:
(binding [*max-value* 500]
(map (bound-fn [x] (valid-value? x)) [299]))
Bye,
Tassilo
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.