【问题标题】:Wrong number of args (2) passed to: core/first传递给的 args (2) 数量错误:core/first
【发布时间】:2016-04-08 09:31:14
【问题描述】:

我是 Clojure 的新手,在迭代数据时遇到了一些问题。

我写的代码如下:

(defn save-monthly-targets
  "Parse Monthly Targets Data and Save"
  [monthlyTargets]
  (println "Save Monthly " monthlyTargets)
  (if (first monthlyTargets)
   (let [month (first monthlyTargets :month)
         year (first monthlyTargets :year)
         name (first monthlyTargets :name)]
        (do
            (println "Calling Save Method" month)
            (users/set-monthly-target month year name)
            (save-monthly-targets (rest monthlyTargets))))))

当我调用函数时:

(save-monthly-targets [
    {:month "May", :year "2021", :target "1200"},
    {:month "May", :year "2016", :target "1200"}
])

我在 (if (firstmonthlyTargets) 语句中得到错误数量的 args 错误。

例外是:

ArityException 错误数量的 args (2) 传递给:core/first clojure.lang.AFn.throwArity

有人能指出这里有什么问题吗?

非常感谢。

【问题讨论】:

    标签: clojure jvm-languages arity


    【解决方案1】:

    如错误所述,您将两个参数传递给first,例如

    (first monthlyTargets :month)
    

    当你想要的时候:

    (let [month (:month (first monthlyTargets))
          ...])
    

    您可以使用解构同时匹配所有值:

    (let [{:keys [month year name]} (first monthlyTargets)
          ...])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多