如何在 Clojure 中对一系列变量应用一系列函数?
How to apply a sequence of functions on a sequence of variables in Clojure?
我有一个函数,它接受一个函数序列和一个参数序列。这应该 return 一个向量,每个函数的结果应用于参数序列。
((solution + max min) 2 3 5 1 6 4) ;;--> [21 6 1]
我试图用 reduce 解决它,但我不知道如何应用它只对第一个函数有效的所有函数:
(defn solution
[& args]
(fn [& args2]
(reduce (first args) [] args2)))
使用juxt
:
((juxt + max min) 2 3 5 1 6 4)
=> [21 6 1]
或定义函数solution
:
(defn solution
[& args]
(fn [& args2]
(apply (apply juxt args) args2)))
((solution + max min) 2 3 5 1 6 4)
=> [21 6 1]
我有一个函数,它接受一个函数序列和一个参数序列。这应该 return 一个向量,每个函数的结果应用于参数序列。
((solution + max min) 2 3 5 1 6 4) ;;--> [21 6 1]
我试图用 reduce 解决它,但我不知道如何应用它只对第一个函数有效的所有函数:
(defn solution
[& args]
(fn [& args2]
(reduce (first args) [] args2)))
使用juxt
:
((juxt + max min) 2 3 5 1 6 4)
=> [21 6 1]
或定义函数solution
:
(defn solution
[& args]
(fn [& args2]
(apply (apply juxt args) args2)))
((solution + max min) 2 3 5 1 6 4)
=> [21 6 1]