Share your thoughts in the 2024 State of Clojure Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in Macros by

Map, Filter and Reduce are very common in functional programming.
Out of all languages that has those functions, only clojure has the capability to add multiple lists on map. for instance- (map + lst1 lst2) which is the most useful thing!

But why isn't it possible to do the same with Filter? for example-

(filter (fn [a1 b1] (pos? (+ a1 b1))) [1 2 3] [-2 4 0]) 

will result in "Wrong number of args(3) passed to clojure.core/filter".
I assume there is a reason for which it is not a common practice, but I find it hard to understand why.

Thanks!

2 Answers

0 votes
by

Not from the Clojure team, but it sounds like a reasonable design choice to me.

For that to work, you would also have to provide a function to combine multiple values into one. One might argue that a vector would make the most sense here, but it's not always the case. That adds unnecessary complexity when you can already do

(filter (fn [[a b]] (pos? (+ a b)))
        (map vector [1 2 3] [-2 4 0]))

But in this case, keep would be a great fit - and there's a recent Ask about it.

0 votes
by

Most preds used with filter take one arg, so this is not commonly needed.

...