1.3.0 permalink Arrow_down_16x16

intersection

clojure.set

  • (intersection s1)
  • (intersection s1 s2)
  • (intersection s1 s2 & sets)
Return a set that is the intersection of the input sets

1 Example top

  • user=> (intersection #{1})
    #{1}
    user=> (intersection #{1 2} #{2 3})
    #{2}
    user=> (intersection #{1 2} #{2 3} #{3 4})
    #{}
    user=> (intersection #{1 :a} #{:a 3} #{:a})
    #{:a}
    
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:32 top

(defn intersection
  "Return a set that is the intersection of the input sets"
  {:added "1.0"}
  ([s1] s1)
  ([s1 s2]
     (if (< (count s2) (count s1))
       (recur s2 s1)
       (reduce (fn [result item]
                   (if (contains? s2 item)
		     result
                     (disj result item)))
	       s1 s1)))
  ([s1 s2 & sets] 
     (let [bubbled-sets (bubble-max-key #(- (count %)) (conj sets s2 s1))]
       (reduce intersection (first bubbled-sets) (rest bubbled-sets)))))
Vars in clojure.set/intersection:
Used in 0 other vars

Comments top

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