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
(defn distinct
"Returns a lazy sequence of the elements of coll with duplicates removed"
{:added "1.0"
:static true}
[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 #{})))
Comments top
No comments for distinct. Log in to add a comment.