【问题标题】:How to apply a sequence of functions on a sequence of variables in Clojure?如何在 Clojure 中的一系列变量上应用一系列函数?
【发布时间】:2021-11-13 20:27:42
【问题描述】:

我有一个函数,它接受一个函数序列和一个参数序列。这应该返回一个向量,其中每个函数的结果应用于参数序列。

((solution + max min) 2 3 5 1 6 4) ;;--> [21 6 1]

我试图用 reduce 解决它,但我不知道如何应用它仅适用于第一个功能的所有功能:

(defn solution
  [& args]
 (fn [& args2]
 (reduce (first args) [] args2)))

【问题讨论】:

    标签: clojure higher-order-functions


    【解决方案1】:

    使用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]
    

    【讨论】:

    • 好吧,(def solution juxt) 可以定义 solution
    猜你喜欢
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多