1.3.0 permalink Arrow_down_16x16

filter

clojure.core

  • (filter pred coll)
Returns a lazy sequence of the items in coll for which
(pred item) returns true. pred must be free of side-effects.

1 Example top

  • user=> (filter even? (range 10))
    (0 2 4 6 8)
    user=> (filter (fn [x]
             (= (count x) 1))
             ["a" "aa" "b" "n" "f" "lisp" "clojure" "q" ""])
    ("a" "b" "n" "f" "q")
    
    user=> (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.
    user=> (filter #(> (second %) 100)
    	       {:a 1
    	        :b 2
    	        :c 101
    	        :d 102
    	        :e -1})
    ([:c 101] [:d 102])
    
    user=> (into {} *1)
    {:c 101, :d 102}
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:2461 top

(defn filter
  "Returns a lazy sequence of the items in coll for which
  (pred item) returns true. pred must be free of side-effects."
  {:added "1.0"
   :static true}
  ([pred coll]
   (lazy-seq
    (when-let [s (seq coll)]
      (if (chunked-seq? s)
        (let [c (chunk-first s)
              size (count c)
              b (chunk-buffer size)]
          (dotimes [i size]
              (when (pred (.nth c i))
                (chunk-append b (.nth c i))))
          (chunk-cons (chunk b) (filter pred (chunk-rest s))))
        (let [f (first s) r (rest s)]
          (if (pred f)
            (cons f (filter pred r))
            (filter pred r))))))))
Vars in clojure.core/filter:
Used in 0 other vars

Comments top

No comments for filter. Log in to add a comment.