Programming Clojure, Third Edition - redundancy in example (page 47)

Good day,

My name is Vadym and currently, I’m reading your book “Programming Clojure, Third Edition”.

I believe I found an example in your book that should be changed/modified.

You can find it on page 47. The function I’m talking about defined as follows:

(defn index-filter [pred coll]

  (when pred

    (for [[idx elt] (indexed coll) :when (pred elt)] idx)))

In this definition (when pred) function call is needless because it’s always evaluated to true (because of the presence of any type of predicate). The absence of predicate pred will cause an arity exception.

The only case when we really need the predicate is for comprehension itself.
From my point of view, the function could be re-written in the next form and the result will be the same (also it will affect SLOCCount and LOC value in the table on the next page, from 6 to 5):

(defn index-filter [pred coll]

  (for [[idx elt] (indexed coll) :when (pred elt)] idx))

Please provide me some additional explanation if I missed something.

1 Like