When to use `constantly` in clojure, how and when are its arguments evaluated?
在另一个问题的可接受答案中,在运行时将Clojure设置为"常量"时,将使用clojure函数
1 2 3 4 | (defn constantly "Returns a function that takes any number of arguments and returns x." {:added"1.0"} [x] (fn [& args] x)) |
doc字符串说明了它的作用,但没有说明为什么要使用它。
在上一个问题给出的答案中,经常使用以下方式:
1 2 3 4 5 6 | (declare version) (defn -main [& args] (alter-var-root #'version (constantly (-> ...))) (do-stuff)) |
因此,不断返回的函数将直接对其结果求值。 我对这很有用感到困惑。 我可能不理解
什么时候应该使用
当API需要一个函数并且您只需要一个常量时,
大多数
编辑:关于问题的第二部分,