I've started doing Project Euler using Clojure, as a first stab at learning Clojure. I've solved the first assignment:
Find the sum of all the multiples of 3 or 5 below 1000.
I've previously solved it in Python:
sum(i for i in xrange(1000) if i%3==0 or i%5==0)
This is my first Clojure attempt:
(reduce +
(filter
(fn [x]
(or
(= 0 (mod x 3))
(= 0 (mod x 5))))
(range 1000)))
I'm actually suprised about how verbose it got, but I'm pretty sure it's because of my style and ignorance of Clojure idioms.
What would an idiomatic version of this Clojure code look like?
This is how I did it:
(apply +
(filter #(or (zero? (mod % 3))
(zero? (mod % 5)))
(range 1000)))
What makes my solution slightly more idiomatic is the use of the anonymous function reader macro, #(...)
and the zero?
fn
Your solution is different but just as good!
BTW - solving euler problems is a great way to learn a new language - you can't get everything from a book.
Edit:
I decided to provide a different solution more inline with your Python version (not very pretty IMO)
(apply +
(for [i (range 1000) :when (or (zero? (mod i 3))
(zero? (mod i 5)))]
i))
Just another version:
(defn sum-of [n]
(reduce + (range n 1000 n)))
(+ (sum-of 3) (sum-of 5) (- (sum-of 15)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With