Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn add [multiset elem] (assoc multiset elem (+ (get multiset elem 0) 1)))
- (defn remove [multiset elem] (case (get multiset elem 0)
- 0 multiset
- 1 (dissoc multiset elem 1)
- (assoc multiset elem (- (get multiset elem) 1))))
- (defn union [multiset other] (reduce
- (fn [m k]
- (assoc m k (+ (get m k 0) (get other k))))
- multiset (keys other)))
- (defn intersect [multiset other] (reduce
- (fn [m k] (if (contains? multiset k) (assoc m k (min (get multiset k) (get other k)))))
- {} (keys other)))
- (defn toString [multiset] (str "Bag[" (subs (reduce
- (fn [s k] (str s ", " k ":" (get multiset k)))
- "" (keys multiset)) 2) "]"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement