There has to be a simple way to do this, and I am obviously missing it 😐
How do you add the items in a list\sequence (not clear on the difference) in clojure?
I’ve tried the following:
Clojure> (add [1 2 3])
java.lang.RuntimeException: Unable to resolve symbol: add in this context
Clojure> (+ [1 2 3])
java.lang.ClassCastException: Cannot cast clojure.lang.PersistentVector to java.lang.Number
Clojure> (apply merge-with + [1 2 3])
java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.lang.Long
Clojure> (add-items [1 2 3])
java.lang.RuntimeException: Unable to resolve symbol: add-items in this context
…will do it. @Nathan Hughes’s solution:
…works if you have a reference to the sequence rather than defining it inline, e.g.:
As @4e6 notes,
reducealso works:Which is better? Opinions vary.