簡體   English   中英

傳遞給:core / first的args(2)數目錯誤

[英]Wrong number of args (2) passed to: core/first

我是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(first firstTargets)語句中得到了錯誤數量的args錯誤。

例外是:

ArityException傳遞給以下參數的錯誤數量的args(2):core / first clojure.lang.AFn.throwArity

有人可以指出這里有什么問題嗎?

非常感謝你。

作為錯誤狀態,您正在將兩個參數傳遞給first例如

(first monthlyTargets :month)

當你想要的時候:

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

您可以使用解構一次匹配所有值:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM