Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么要在clojure.core中实现partial_Clojure - Fatal编程技术网

为什么要在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核心库倾向于认真对待性能,前提是该语言的基本构建块将出现在每个人的性能瓶颈中