Skip to content

Instantly share code, notes, and snippets.

@hoeck hoeck/multiset
Created May 23, 2010

Embed
What would you like to do?
(deftype Multiset [m]
clojure.lang.IPersistentSet
(get [_ k] (when-let [[k v] (find m k)]
(when (< 0 v) k)))
(contains [_ k] (when-let [[k v] (find m k)]
(when (< 0 v) true)))
(disjoin [_ k] (Multiset. (update-in m [k] #(if % (if (< 0 %) (dec %) 0) 0))))
(count [t] (count (seq t)))
(cons [_ k] (Multiset. (update-in m [k] #(if % (inc %) 1))))
(empty [t] (Multiset. {}))
(equiv [_ mm] (if (instance? Multiset mm) (= (.m mm) m) false))
(seq [_] (mapcat (fn [[k v]] (repeat v k)) m)))
(let [ms (Multiset. {})]
(clojure.set/difference (conj ms :a :a :a :b :b :b :b)
(conj ms :a :a :a :b :b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.