nobody_nowhere
nobody_nowhere

Reputation: 81

Clojure - User implementation of Filter

I am a newcomer to Clojure and one of the challenges suggested is a user-implementation of the Filter function.

So I came up with this

 (defn filterr
  "Filter implementation"
  [condition coll]
  (if-not (empty? coll)
    (if (condition (first coll))
      (cons (first coll) (filterr condition (rest coll)))
      (filterr (condition (rest coll))))))
(defn -main
  "Main" []
  (t/is (=
         (filterr #(> % 2) [1 2 3 4 5 6])
         [3 4 5 6])
        "Failed basic test"))

However, my test fails with the error

ERROR in () (Numbers.java:229) Failed basic test expected: (= (filterr (fn* [p1__42#] (> p1__42# 2)) [1 2 3 4 5 6]) [3 4 5 6])

It seems like the function isn't being evaluated fully.

I don't see what I'm doing wrong, and would really appreciate some help on the matter.

Upvotes: 2

Views: 165

Answers (2)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

there's an extra set of ( ) in the then clause of the if statement.

(filterr (condition (rest coll)))

vs

(filterr condition (rest coll))

Upvotes: 5

akond
akond

Reputation: 16035

The error is in this line (filterr (condition (rest coll))))))

You ought to have (filterr condition (rest coll))))) instead. Because (condition (rest coll)) make it a function call, while you simply need to pass this parameter to the next filterr call.

Upvotes: 2

Related Questions