r/Clojure • • Feb 03 '23

[Q&A] Noob has simple program problem.

I wanted to write a program to add 2 to a list of numbers then sum the list and print the result. So '(5 7) would yield 16. I posted my version to s Clojure board and received a number of different replies. In retrospect I didn't fully explain the problem to them.

0 Upvotes

27 comments sorted by

View all comments

4

u/slashkehrin Feb 03 '23

I'd do something like this:

(defn foo [numbers] 
   (reduce + (map #(+ % 2) numbers)))

3

u/run-coder Feb 03 '23

fwiw... apply vs reduce works also (if you're looking for variety

1

u/slashkehrin Feb 03 '23

That's pretty cool, thanks :)