據我所知,在動態變量上設置新綁定會影響綁定中調用的所有函數以及從這些函數調用的所有函數。動態變量的Clojure綁定無法按預期方式工作
爲什麼綁定在下面的第一個示例中似乎丟失? map
返回其內部的結合定義,但外評價懶惰序列 -
(def ^:dynamic *out-dir* "/home/user")
(binding [*out-dir* "/home/dave"] (map #(str *out-dir* %) [1 2 3]))
; gives: ("/home/user1" "/home/user2" "/home/user3")
; expected: ("/home/dave1" "/home/dave2" "/home/dave3")
(binding [*out-dir* "/home/dave"] (conj (map #(str *out-dir* %) [1 2 3]) *out-dir*))
; gives: ("/home/dave" "/home/dave1" "/home/dave2" "/home/dave3")
嗯,就是這樣。能夠在這裏找到更多有關綁定陷阱的更多信息:[鏈接](http://cemerick.com/2009/11/03/be-mindful-of-clojures-binding/) –
@DavidVail,請注意,事情自撰寫博客文章以來已有所改進(僅在一個月後,Clojure 1.1引入了「bound-fn」)。 – galdre