This section quickly shows how to select only some elements of a Clojure sequence.
The first parts of the recipe do not need any special preparation, but the final section needs the core.async
library to be added to your project.clj
file (or any other dependency management you are using):
:dependencies [ [org.clojure/clojure "1.8.0"] [org.clojure/core.async "0.2.371"]]
There are four main functions to filter elements:
filter
keep
andkeep-indexed
remove
take
andtake-while
Let's go through a few simple examples.
The following code filters numbers that are multiples of three:
(filter #(= 0 (rem % 3)) (range 1 10)) ; (3 6 9)
This filters keys that are in the map, which is used as a function here:
(filter {:b 2 :c 3} [:a :b]) ; (:b)