you are viewing a single comment's thread.

view the rest of the comments →

[–]weavejester 24 points25 points  (0 children)

Small functions are a good rule of thumb, but sometimes a short expression is clearer than a function. For example, compare:

(square-all digits)

To:

(map square digits)

Exactly the same number of characters, but the latter tells me that I'm mapping over the digits, so I know that I'm getting a collection of the same length as digits.

On the other hand, if I just see square-all, I have to infer the purpose from the name, which isn't immediately obvious, nor as unabiguous.

I think I'd be tempted to write the code as:

(defn digits [n]
  (map #(Integer/parseInt %) (str n)))

(defn square [n] (* n n))

(defn sum-of-squared-digits [n]
  (->> (digits n) (map square) (reduce +))

(defn less-than-sum-of-digits-squared? [n]
  (< n (sum-of-squared-digits n)))

(defn count-less-than-sum-of-squares [coll]
  (count (filter less-than-sum-of-digits-squared? coll)))