difference

clojure.set

  • (difference s1)
  • (difference s1 s2)
  • (difference s1 s2 & sets)
Return a set that is the first set without elements of the remaining sets

2 Examples top

  • user=> (difference #{1 2 3})
    #{1 2 3}
    user=> (difference #{1 2} #{2 3})
    #{1}
    user=> (difference #{1 2 3} #{1} #{1 4} #{3})
    #{2}
  • user=> (difference (set (keys {:1 1 :2 2 :3 3})) (set (keys {:1 1 :2 2})))
    #{:3}
    user=> (difference (set (keys {:1 1 :2 2})) (set (keys {:1 1 :2 2 :3 3})))
    #{}
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/set.clj:48 top

(defn difference
  "Return a set that is the first set without elements of the remaining sets"
  {:added "1.0"}
  ([s1] s1)
  ([s1 s2] 
     (if (< (count s1) (count s2))
       (reduce (fn [result item] 
                   (if (contains? s2 item) 
                     (disj result item) 
                     result))
               s1 s1)
       (reduce disj s1 s2)))
  ([s1 s2 & sets] 
     (reduce difference s1 (conj sets s2))))
Vars in clojure.set/difference:
Used in 0 other vars

Comments top

No comments for difference. Log in to add a comment.