ClojureDocs

Nav

Namespaces

union

clojure.set

Available since 1.0
  • (union)
  • (union s1)
  • (union s1 s2)
  • (union s1 s2 & sets)
Return a set that is the union of the input sets
6 Examples
user=> (union)
#{}

user=> (union #{1 2})
#{1 2}

user=> (union #{1 2} #{2 3})
#{1 2 3}

user=> (union #{1 2} #{2 3} #{3 4})
#{1 2 3 4}
(reduce (fn [flattened [key val]]
          (conj flattened val))
        #{}
        {:e #{:m :f}, :c #{:f}, :b #{:c :f}, :d #{:m :f}, :a #{:c :f}})

;;=> #{#{:m :f} #{:c :f} #{:f}}


(reduce (fn [flattened [key val]]
          (clojure.set/union flattened val))
        #{}
        {:e #{:m :f}, :c #{:f}, :b #{:c :f}, :d #{:m :f}, :a #{:c :f}})

;;=> #{:m :c :f}
(defn flatten-dpdnts [dpdnts-map]
  (apply set/union (vals dpdnts-map)))

(flatten-dpdnts {:e #{:m :f}, :c #{:f}, :b #{:c :f}, :d #{:m :f}, :a #{:c :f}})
;;=> #{:m :c :f}
;; Advice: Do not call union with non-set arguments.  If you are
;; concerned that you may be unintentionally doing so, and want an
;; exception to be thrown if you do, consider using the library
;; funjible (https://github.com/jafingerhut/funjible) which provides
;; its own definition of union that is identical to Clojure's, except
;; it checks the types of its arguments.

;; union might or might not return what you expect if you give it
;; values that are not sets.  The implementation of union _does not
;; check_ whether you are actually giving it values that are sets.  It
;; _assumes_ so.

;; This looks like what someone might expect.  It _happens_ to give
;; back the same answer as if you coerced the second argument to a
;; set.
user=> (union #{1 2 3} [4 5])
#{1 4 3 2 5}

;; Wait, this returned a vector, not a set!
user=> (union #{1 2} [3 4 5])
[3 4 5 1 2]

;; This also returned a vector, and some elements are duplicates of
;; each other!
user=> (union #{1 2} [3 4 5] #{4 5})
[3 4 5 1 2 4 5]

;; Why not change the definition of union so it throws an exception if
;; you give it a non-set argument?  I would guess that the primary
;; reason is that the extra run-time type checks would slow union down
;; by an amount that the Clojure core team does not want everyone to
;; have to pay on every such call.

;; Related Clojure tickets: 
;; https://dev.clojure.org/jira/browse/CLJ-1953
;; https://dev.clojure.org/jira/browse/CLJ-2287
;; (set/union) treats nil as the empty set, as you'd probably expect.
(clojure.set/union nil #{1 2} nil)
;;=> #{1 2}

;; This makes it handy to use as an accumulator in combination with (update-in):
(update-in {} [:a :b :c] clojure.set/union #{333})
;;=> {:a {:b {:c #{333}}}}
;; The "powerset" is the set of all the combinations of "items"
(require '[clojure.set :refer [union]])

(defn powerset [items]
  (reduce
    (fn [s x]
      (union s (map #(conj % x) s)))
    (hash-set #{})
    items))

(powerset #{1 2 3})
;; #{#{} #{3} #{2} #{1} #{1 3 2} #{1 3} #{1 2} #{3 2}}
See Also

Return a set that is the intersection of the input sets

Added by mmwaikar

Return a set that is the first set without elements of the remaining sets

Added by mmwaikar

Is set1 a superset of set2?

Added by mmwaikar
0 Notes
No notes for union