[英]clojure.core/or partial function works well with first argument but not with second
[英]Why such implementation of partial in clojure.core
我在cojure.core
偶然發現了partial
功能的cojure.core
。 它看起來像這樣:
(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核心庫往往會認真對待性能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.