4Clojure Problem 58表述爲:Clojure的:實現補償功能
寫一個函數,它允許你創建的功能成分。參數列表應該包含可變數量的函數,並且創建一個函數從右到左應用它們。
(= [3 2 1] ((__ rest reverse) [1 2 3 4]))
(= 5 ((__ (partial + 3) second) [1 2 3 4]))
(= true ((__ zero? #(mod % 8) +) 3 5 7 9))
(= "HELLO" ((__ #(.toUpperCase %) #(apply str %) take) 5 "hello world"))
這裏__
應該被解決方案替換。
在這個問題中,不應該使用函數comp
。
我找到了一個解決辦法是:
(fn [& xs]
(fn [& ys]
(reduce #(%2 %1)
(apply (last xs) ys) (rest (reverse xs)))))
它的工作原理。但我並不十分了解reduce
在這裏的工作原理。它代表(apply f_1 (apply f_2 ...(apply f_n-1 (apply f_n args))...)
?
我真的很喜歡這個分解樣稿成鏈,降低 - 這是很容易理解。 – Mala