faster distinct: maintain seen items in a set

* hy/core/language.hy: maintain the seen items in a set instead of a
  list in `distinct`. This is much faster for lookups.
This commit is contained in:
Abhishek L 2014-02-04 01:35:41 +05:30
parent e8dfe5bfb2
commit a41a3c7edc

View File

@ -75,12 +75,12 @@
(defn distinct [coll]
"Return a generator from the original collection with duplicates
removed"
(let [[seen []] [citer (iter coll)]]
(let [[seen (set)] [citer (iter coll)]]
(for* [val citer]
(if (not_in val seen)
(do
(yield val)
(.append seen val))))))
(.add seen val))))))
(defn drop [count coll]
"Drop `count` elements from `coll` and yield back the rest"