I am new to Clojure and I have having some issues with iterating data.
The code I have written is below :
(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))))))
When I call the function:
(save-monthly-targets [
{:month "May", :year "2021", :target "1200"},
{:month "May", :year "2016", :target "1200"}
])
I get the wrong number of args error in the (if (first monthlyTargets) statement.
The exception is:
ArityException Wrong number of args (2) passed to: core/first clojure.lang.AFn.throwArity
Can someone point what is wrong here?
Thank you very much.
As the error states, you are passing two parameters to first
e.g.
(first monthlyTargets :month)
when you want:
(let [month (:month (first monthlyTargets))
...])
you can match all the values at once using destructuring:
(let [{:keys [month year name]} (first monthlyTargets)
...])