C-redefs on functions used inside maps

I was wondering what happens in this snippet below. Why is the function not overridden without having to evaluate the sequence?

user> (defn foo [] (map vector (range 3)))
#'user/foo
user> (defn bar [] (map #(vector %) (range 3)))
#'user/bar
user> (foo)
([0] [1] [2])
user> (bar)
([0] [1] [2])
user> (with-redefs [vector (fn [_] "what does the fox say?")] (foo))
("what does the fox say?" "what does the fox say?" "what does the fox say?")
user> (with-redefs [vector (fn [_] "what does the fox say?")] (bar))
([0] [1] [2])
user> (with-redefs [vector (fn [_] "what does the fox say?")] (vec (bar)))
["what does the fox say?" "what does the fox say?" "what does the fox say?"]
user> 

Thanks!

+4
source share
1 answer

The difference is that when you call foo, vectoras an argument map, it is evaluated (which in this case means allowing it to be a function object) once and does not need to be resolved again. The same function object is used even after your code exits with-redefs.

bar, , vector map, , vector . , , vector . map , , with-redefs ( , ).

, - (map vector (range 3)) - , . , map foo vector, map bar , - vector .

Clojure.org , . .

+6

All Articles