【发布时间】:2010-10-25 17:00:05
【问题描述】:
在另一个问题的已接受答案中,Setting Clojure "constants" at runtime 使用了 clojure 函数 constantly。
constantly 的定义如下:
(defn constantly
"Returns a function that takes any number of arguments and returns x."
{:added "1.0"}
[x] (fn [& args] x))
文档字符串说明了它的作用,但没有说明为什么要使用它。
在上一题给出的答案中不断地使用如下:
(declare version)
(defn -main
[& args]
(alter-var-root #'version (constantly (-> ...)))
(do-stuff))
因此,由 constant 返回的函数直接对其结果进行评估。我对这有什么用感到困惑。我可能不明白 x 在是否被包裹在“不断地”中的情况下如何被评估。
什么时候应该使用constantly,为什么需要?
【问题讨论】:
标签: clojure evaluation