The first higher-order function in Clojure I will look at is filter. It takes a predicate and a collection. It returns all the members of the collection for which the predicate is true. The docs say that it takes a “predicate”, but that predicate duty is frequently fulfilled by a function.
I tried calling filter with “true” as the predicate, and it did not work. I guess in Clojure (and probably all Lisps), a predicate is just a function that returns a boolean.
Clojure has two versions of filter: filter and filterv. The difference is that filter returns a lazy sequence, and filterv returns a vector.
user> (filter even? [1 2 3 4]) (2 4) user> (filterv even? [1 2 3 4]) [2 4]
If filter is called with just the predicate, it will return a transducer. I will cover transducers later.
There are also another “filter” function in the clojure.core.reducers namespace. It takes the same arguments as the core filter, but when I tried it in a REPL I got an error. After looking at the Clojure page on reducers, I think the functions in that namespace only work if you chain them together.