Question

I'm trying to figure why this particular function isn't working as expected. I suspect from the error message that it has something to do with the way I'm creating the empty vector for the accumulator.

I have a simple function that returns a sequence of 2-element vectors:

(defn zip-with-index
  "Returns a sequence in which each element is of the
   form [i c] where i is the index of the element and c
   is the element at that index."
   [coll]
   (map-indexed (fn [i c] [i c]) coll))

That works fine. The problem comes when I try to use it in another function

(defn indexes-satisfying
  "Returns a vector containing all indexes of coll that satisfy
   the predicate p."
  [p coll]
  (defn accum-if-satisfies [acc zipped]
    (let [idx (first zipped)
          elem (second zipped)]
      (if (p elem) 
        (conj acc idx)
        (acc))))
  (reduce accum-if-satisfies (vector) (zip-with-index coll)))

It compiles, but when I attempt to use it I get an error:

user=> (indexes-satisfying (partial > 3) [1 3 5 7])
ArityException Wrong number of args (0) passed to: PersistentVector
clojure.lang.AFn.throwArity (AFn.java:437)

I can't figure out what's going wrong here. Also if there is a more 'Clojure-like' way of doing what I'm trying to do, I'm interested in hearing about that also.

Was it helpful?

Solution

The problem is probably on the else clause of accum-if-satisfies, should be just acc not (acc).

You could use filter and then map instead of reduce. Like that:

(map #(first %) 
     (filter #(p (second %))
             (zip-with-index coll)))

You could also call map-indexed with vector instead of (fn [i c] [i c]). The whole code would look like that:

(defn indexes-satisfying
  [p coll]
  (map #(first %)
       (filter #(p (second %))
               (map-indexed vector coll))))

OTHER TIPS

As for a more Clojure-like way, you could use

(defn indexes-satisfying [pred coll]
  (filterv #(pred (nth coll %))
           (range (count coll))))

Use filter instead of filterv to return a lazy seq rather than a vector.

Also, you should not use defn to define inner functions; it will instead define a global function in the namespace where the inner function is defined and have subtle side effects besides that. Use letfn instead:

(defn outer [& args]
  (letfn [(inner [& inner-args] ...)]
    (inner ...)))

One more way to do it would be:

(defn indexes-satisfying [p coll]
  (keep-indexed #(if (p %2) % nil) coll))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top