abo-abo
abo-abo

Reputation: 20342

Clojure: combination of binding and map acting weird?

I'm learning Clojure at the moment, and I'm not getting the logic behind this code:

(def ^:dynamic *max-value* 250)
(defn valid-value? [v]
  (<= v *max-value*))

(binding [*max-value* 500]
  (prn (map valid-value? [299]))
  (map valid-value? [299]))

It prints (true), but returns (false). I realized the answer as I finished typing. I guess I'll post the question anyway, maybe it will be useful for someone else.

Upvotes: 2

Views: 83

Answers (1)

deprecated
deprecated

Reputation: 5242

map generates a lazy sequence, which evaluation isn't forced until the repl prints the value, in this case.

At that point, *max-value* is no longer bound to 500.

If you use mapv instead, [true] will be returned!

Upvotes: 2

Related Questions