为什么要在clojure.core中实现partial
我在为什么要在clojure.core中实现partial,clojure,Clojure,我在cojure.core中偶然发现了partial函数的实现。看起来是这样的: (defn partial "Takes a function f and fewer than the normal arguments to f, and returns a fn that takes a variable number of additional args. When called, the returned function calls f with args + additi
cojure.core
中偶然发现了partial
函数的实现。看起来是这样的:
(defn partial
"Takes a function f and fewer than the normal arguments to f, and
returns a fn that takes a variable number of additional args. When
called, the returned function calls f with args + additional args."
{:added "1.0"
:static true}
([f] f)
([f arg1]
(fn [& args] (apply f arg1 args)))
([f arg1 arg2]
(fn [& args] (apply f arg1 arg2 args)))
([f arg1 arg2 arg3]
(fn [& args] (apply f arg1 arg2 arg3 args)))
([f arg1 arg2 arg3 & more]
(fn [& args] (apply f arg1 arg2 arg3 (concat more args)))))
如果可以的话,为什么它有几个平价选项?它只是性能优化,所以在大多数情况下不会调用concat
我的意思是它可能看起来像这样,不是吗
(defn partial
([f] f)
([f & more]
(fn [& args] (apply f (concat more args))))
)
我还注意到其他几个函数也遵循相同的模式。是的,这是一种性能优化 我不仅仅是不调用
concat
,而是因为参数列表中的&
也需要创建一个集合。clojure核心库倾向于认真对待性能,前提是该语言的基本构建块将出现在每个人的性能瓶颈中