You're viewing version 1.2.0 of distinct. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

distinct

clojure.core

  • (distinct coll)
Returns a lazy sequence of the elements of coll with duplicates removed

2 Examples top

  • user=> (distinct [1 2 1 3 1 4 1 5])
    (1 2 3 4 5)
  • user=> (def fractions 
             (for [n (range 1 100) d (range (inc n) 100)] 
               (let [gcd (clojure.contrib.math/gcd n d)] 
                 (/ (/ n gcd) (/ d gcd)))))
    ;; all irreducible fractions with denominator < 100
    ;; (1/2 1/3 ... 1/99 2/3 1/2 2/5 1/3 ...)
    
    user=> (count fractions)
    4851
    
    user=> (count (distinct fractions))
    3003
    
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:3963 top

(defn distinct
  "Returns a lazy sequence of the elements of coll with duplicates removed"
  {:added "1.0"}
  [coll]
    (let [step (fn step [xs seen]
                   (lazy-seq
                    ((fn [[f :as xs] seen]
                      (when-let [s (seq xs)]
                        (if (contains? seen f) 
                          (recur (rest s) seen)
                          (cons f (step (rest s) (conj seen f))))))
                     xs seen)))]
      (step coll #{})))
Vars in clojure.core/distinct: defn let
Used in 2 other vars find-symbols find-holes

Comments top

1 comment(s) for distinct.

If you do not need the lazyness of distinct, set can be faster. Like: (count (set some-coll)).

Log in to add a comment.