Returns a lazy sequence of the items in coll for which (pred item) returns logical true. pred must be free of side-effects. Returns a transducer when no collection is provided.
(filter even? (range 10)) ;;=> (0 2 4 6 8) (filter (fn [x] (= (count x) 1)) ["a" "aa" "b" "n" "f" "lisp" "clojure" "q" ""]) ;;=> ("a" "b" "n" "f" "q") (filter #(= (count %) 1) ["a" "aa" "b" "n" "f" "lisp" "clojure" "q" ""]) ;;=> ("a" "b" "n" "f" "q") ; When coll is a map, pred is called with key/value pairs. (filter #(> (second %) 100) {:a 1 :b 2 :c 101 :d 102 :e -1}) ;;=> ([:c 101] [:d 102]) (into {} *1) ;;=> {:c 101, :d 102}
;; Used without a collection, filter will create a transducer: (def xf (filter odd?)) ;; We can now apply this transducer to a sequence: (transduce xf conj (range 10)) ;; => [1 3 5 7 9]
;;When filtering a map, the predicate takes a _list_ of length 2 (filter (fn [[k v]] (even? k)) {1 "a", 2 "b", 3 "c", 4 "d"} ) ;;output:: ([2 "b"] [4 "d"]) ;;A function of arity two will cause an error (comment will fail!) (filter (fn [k v] (even? k)) {1 "a", 2 "b", 3 "c", 4 "d"} ) ;;output:: clojure.lang.ArityException: Wrong number of args (1) passed to: ...
; remove empty vectors from the root vector (def vector-of-vectors [[1 2 3] [] [1] []]) (def populated-vector? (fn [item] (not= item []))) (filter populated-vector? vector-of-vectors) ; => ([1 2 3] [1]) ; or (filter seq vector-of-vectors)
;;filter a map on its values (filter (comp #{2 3} last) {:x 1 :y 2 :z 3}) ;;=> ([:y 2] [:z 3]) ;;extract keys for certain values (map first (filter (comp #{2 3} last) {:x 1 :y 2 :z 3})) => (:y :z)
;; You can use set as a filter predicate. In this case it is sets intersection (filter #{0 1 2 3} #{2 3 4 5}) => (3 2)
;; That's how to get everything from a seq that is not nil; (filter some? '(1 nil [] :a nil)) => (1 [] :a)
;;practical example using an anonymous function ;;which return a boolean value (def entries [{:month 1 :val 12 :s1 true :s2 false} {:month 2 :val 3 :s1 false :s2 true} {:month 3 :val 32 :s1 true :s2 false} {:month 4 :val 18 :s1 true :s2 false} {:month 5 :val 32 :s1 false :s2 true} {:month 6 :val 62 :s1 false :s2 true} {:month 7 :val 12 :s1 false :s2 true} {:month 8 :val 142 :s1 true :s2 false} {:month 9 :val 52 :s1 true :s2 false} {:month 10 :val 18 :s1 true :s2 false} {:month 11 :val 23 :s1 false :s2 true} {:month 12 :val 56 :s1 false :s2 true}]) (filter #(:s2 %) entries) (filter #(and (:s2 %) (> (:val %) 30)) entries)
;; given users ;; ["pinisi" "sikad" "zenius" "teacher" "law" "calvin" "ijul"] ;; ;; kick "law" and "teacher" (filter #(not (some (fn [u] (= u %)) ["law" "teacher"])) ["pinisi" "sikad" "zenius" "teacher" "law" "calvin" "ijul"])
;; if you want to apply multiple predicates, use every-pred ;; in conjunction with filter ;; output: (2, 4) (filter (apply every-pred [even? #(< % 5)]) [1, 2, 3, 4, 5]) ;; if you want to apply any predicate, use some-fn ;; output: (0 2 4 6 7 8 9) (filter (some-fn even? #(> % 5)) (range 10))
Returns a lazy sequence of the items in coll for which (pred item) returns logical false. pred mus...
Returns a lazy sequence of the non-nil results of (f item). Note, this means false return values w...
Returns a vector of the items in coll for which (pred item) returns logical true. pred must be fre...
Returns a map of the elements of coll keyed by the result of f on each element. The value at each ...
Although the documentation states that the predicate must be free of side effects, it would be more accurate to say that you should not rely on filter to induce side effects that may be caused by the predicate, nor on the timing of when the predicate will be evaluated, because of the lazy and chunked nature of filter.