Back
intersection (clj)
(source)function
(intersection s1)
(intersection s1 s2)
(intersection s1 s2 & sets)
Return a set that is the intersection of the input sets
Examples
clojure
(ns clojure.test-clojure.clojure-set
(:use clojure.test)
(:require [clojure.set :as set]))
(deftest test-intersection
; at least one argument is needed
(is (thrown? IllegalArgumentException (set/intersection)))
(are [x y] (= x y)
; identity
(set/intersection #{}) #{}
(set/intersection #{1}) #{1}
(set/intersection #{1 2 3}) #{1 2 3}
; 2 sets, at least one is empty
(set/intersection #{} #{}) #{}
(set/intersection #{} #{1}) #{}
(set/intersection #{} #{1 2 3}) #{}
(set/intersection #{1} #{}) #{}
(set/intersection #{1 2 3} #{}) #{}
; 2 sets
(set/intersection #{1 2} #{1 2}) #{1 2}
(set/intersection #{1 2} #{3 4}) #{}
(set/intersection #{1 2} #{1}) #{1}
(set/intersection #{1 2} #{2}) #{2}
(set/intersection #{1 2 4} #{2 3 4 5}) #{2 4}
; 3 sets, some are empty
(set/intersection #{} #{} #{}) #{}
(set/intersection #{1} #{} #{}) #{}
(set/intersection #{1} #{1} #{}) #{}
(set/intersection #{1} #{} #{1}) #{}
(set/intersection #{1 2} #{2 3} #{}) #{}
; 3 sets
(set/intersection #{1 2} #{2 3} #{5 2}) #{2}
(set/intersection #{1 2 3} #{1 3 4} #{1 3}) #{1 3}
(set/intersection #{1 2 3} #{3 4 5} #{8 2 3}) #{3}
; different types of sets
(set/intersection (hash-set 1 2) (hash-set 2 3)) #{2}
(set/intersection (sorted-set 1 2) (sorted-set 2 3)) #{2}
(set/intersection
(hash-set 1 2) (hash-set 2 3)
(sorted-set 1 2) (sorted-set 2 3)) #{2} ))
clojure
(ns clojure.test-clojure.metadata
(:use clojure.test
[clojure.test-helper :only (eval-in-temp-ns)])
(:require [clojure.set :as set]))
(deftest interaction-of-def-with-metadata
(testing "initial def sets metadata"
(let [v (eval-in-temp-ns
(def ^{:a 1} foo 0)
#'foo)]
(is (= 1 (-> v meta :a)))))
(testing "const vars preserve metadata"
(let [[v1 v2] (eval-in-temp-ns
(def ^:const foo ^:foo [])
(def ^:const bar ^:foo [:bar])
[(meta foo) (meta bar)])]
(is (= {:foo true} v1))
(is (= {:foo true} v2))))
#_(testing "subsequent declare doesn't overwrite metadata"
(let [v (eval-in-temp-ns
(def ^{:b 2} bar 0)
(declare bar)
#'bar)]
(is (= 2 (-> v meta :b))))
(testing "when compiled"
(let [v (eval-in-temp-ns
(def ^{:c 3} bar 0)
(defn declare-bar []
(declare bar))
(declare-bar)
#'bar)]
(is (= 3 (-> v meta :c))))))
(testing "subsequent def with init-expr *does* overwrite metadata"
(let [v (eval-in-temp-ns
(def ^{:d 4} quux 0)
(def quux 1)
#'quux)]
(is (nil? (-> v meta :d))))
(testing "when compiled"
(let [v (eval-in-temp-ns
(def ^{:e 5} quux 0)
(defn def-quux []
(def quux 1))
(def-quux)
#'quux)]
(is (nil? (-> v meta :e))))))
(testing "IllegalArgumentException should not be thrown"
(testing "when defining var whose value is calculated with a primitive fn."
(testing "This case fails without a fix for CLJ-852"
(is (eval-in-temp-ns
(defn foo ^long [^long x] x)
(def x (inc (foo 10))))))
(testing "This case should pass even without a fix for CLJ-852"
(is (eval-in-temp-ns
(defn foo ^long [^long x] x)
(def x (foo (inc 10)))))))))
(deftest fns-preserve-metadata-on-maps
(let [xm {:a 1 :b -7}
x (with-meta {:foo 1 :bar 2} xm)
ym {:c "foo"}
y (with-meta {:baz 4 :guh x} ym)]
(is (= xm (meta (:guh y))))
(is (= xm (meta (reduce #(assoc %1 %2 (inc %2)) x (range 1000)))))
(is (= xm (meta (-> x (dissoc :foo) (dissoc :bar)))))
(let [z (assoc-in y [:guh :la] 18)]
(is (= ym (meta z)))
(is (= xm (meta (:guh z)))))
(let [z (update-in y [:guh :bar] inc)]
(is (= ym (meta z)))
(is (= xm (meta (:guh z)))))
(is (= xm (meta (get-in y [:guh]))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (merge x y))))
(is (= ym (meta (merge y x))))
(is (= xm (meta (merge-with + x y))))
(is (= ym (meta (merge-with + y x))))
(is (= xm (meta (select-keys x [:bar]))))
(is (= xm (meta (set/rename-keys x {:foo :new-foo}))))
;; replace returns a seq when given a set. Can seqs have
;; metadata?
;; TBD: rseq, subseq, and rsubseq returns seqs. If it is even
;; possible to put metadata on a seq, does it make sense that the
;; seqs returned by these functions should have the same metadata
;; as the sorted collection on which they are called?
))
(deftest fns-preserve-metadata-on-vectors
(let [xm {:a 1 :b -7}
x (with-meta [1 2 3] xm)
ym {:c "foo"}
y (with-meta [4 x 6] ym)]
(is (= xm (meta (y 1))))
(is (= xm (meta (assoc x 1 "one"))))
(is (= xm (meta (reduce #(conj %1 %2) x (range 1000)))))
(is (= xm (meta (pop (pop (pop x))))))
(let [z (assoc-in y [1 2] 18)]
(is (= ym (meta z)))
(is (= xm (meta (z 1)))))
(let [z (update-in y [1 2] inc)]
(is (= ym (meta z)))
(is (= xm (meta (z 1)))))
(is (= xm (meta (get-in y [1]))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (replace {2 "two"} x))))
(is (= [1 "two" 3] (replace {2 "two"} x)))
;; TBD: Currently subvec drops metadata. Should it preserve it?
;;(is (= xm (meta (subvec x 2 3))))
;; TBD: rseq returns a seq. If it is even possible to put
;; metadata on a seq, does it make sense that the seqs returned by
;; these functions should have the same metadata as the sorted
;; collection on which they are called?
))
(deftest fns-preserve-metadata-on-sets
;; TBD: Do tests independently for set, hash-set, and sorted-set,
;; perhaps with a loop here.
(let [xm {:a 1 :b -7}
x (with-meta #{1 2 3} xm)
ym {:c "foo"}
y (with-meta #{4 x 6} ym)]
(is (= xm (meta (y #{3 2 1}))))
(is (= xm (meta (reduce #(conj %1 %2) x (range 1000)))))
(is (= xm (meta (-> x (disj 1) (disj 2) (disj 3)))))
(is (= xm (meta (into x y))))
(is (= ym (meta (into y x))))
(is (= xm (meta (set/select even? x))))
(let [cow1m {:what "betsy cow"}
cow1 (with-meta {:name "betsy" :id 33} cow1m)
cow2m {:what "panda cow"}
cow2 (with-meta {:name "panda" :id 34} cow2m)
cowsm {:what "all the cows"}
cows (with-meta #{cow1 cow2} cowsm)
cow-names (set/project cows [:name])
renamed (set/rename cows {:id :number})]
(is (= cowsm (meta cow-names)))
(is (= cow1m (meta (first (filter #(= "betsy" (:name %)) cow-names)))))
(is (= cow2m (meta (first (filter #(= "panda" (:name %)) cow-names)))))
(is (= cowsm (meta renamed)))
(is (= cow1m (meta (first (filter #(= "betsy" (:name %)) renamed)))))
(is (= cow2m (meta (first (filter #(= "panda" (:name %)) renamed))))))
;; replace returns a seq when given a set. Can seqs have
;; metadata?
;; union: Currently returns the metadata of the largest input set.
;; This is an artifact of union's current implementation. I doubt
;; any explicit design decision was made to do so. Like join,
;; there doesn't seem to be much reason to prefer the metadata of
;; one input set over another, if at least two input sets are
;; given, but perhaps defining it to always return a set with the
;; metadata of the first input set would be reasonable?
;; intersection: Returns metadata of the smallest input set.
;; Otherwise similar to union.
;; difference: Seems to always return a set with metadata of first
;; input set. Seems reasonable. Not sure we want to add a test
;; for it, if it is an accident of the current implementation.
;; join, index, map-invert: Currently always returns a value with
;; no metadata. This seems reasonable.
))
functional-koans/clojure-koans
(ns koans.05-sets
(:require [koan-engine.core :refer :all]
[clojure.set :as set]))
"And also the intersection"
(= __ (set/intersection #{1 2 3 4} #{2 3 5}))
clojure/core.typed
(ns ^:skip-wiki clojure.core.typed.bootstrap-cljs
(:require [clojure.set :as set]))
(let [i (set/intersection -base-aliases -specials)]
(assert (empty? i) (pr-str i)))
mtgred/netrunner
(ns game.cards.hardware
(:require
[clojure.set :as set]
[clojure.string :as str]
[game.core.access :refer [access-bonus access-card breach-server
get-only-card-to-access]]
[game.core.actions :refer [play-ability]]
[game.core.board :refer [all-active all-active-installed all-installed]]
[game.core.card :refer [active? corp? event? facedown? get-card get-counters get-title
get-zone hardware? has-subtype? ice? in-deck? in-discard?
in-hand? in-scored? installed? program? resource? rezzed?
runner? virus-program? faceup?]]
[game.core.card-defs :refer [card-def]]
[game.core.cost-fns :refer [all-stealth install-cost
rez-additional-cost-bonus rez-cost trash-cost]]
[game.core.damage :refer [chosen-damage damage damage-prevent
enable-runner-damage-choice runner-can-choose-damage?]]
[game.core.def-helpers :refer [breach-access-bonus defcard offer-jack-out
reorder-choice trash-on-empty get-x-fn]]
[game.core.drawing :refer [draw]]
[game.core.effects :refer [register-lingering-effect
unregister-effects-for-card unregister-lingering-effects]]
[game.core.eid :refer [effect-completed make-eid make-result]]
[game.core.engine :refer [can-trigger? not-used-once? register-events
register-once register-suppress resolve-ability trigger-event
unregister-floating-events unregister-suppress-by-uuid]]
[game.core.events :refer [event-count first-event? first-trash? no-event?
run-events]]
[game.core.expose :refer [expose]]
[game.core.finding :refer [find-card find-latest]]
[game.core.flags :refer [can-trash? card-flag? in-corp-scored? register-run-flag!
zone-locked?]]
[game.core.gaining :refer [gain-clicks gain-credits lose-clicks
lose-credits]]
[game.core.hand-size :refer [hand-size runner-hand-size+]]
[game.core.hosting :refer [host]]
[game.core.ice :refer [all-subs-broken? any-subs-broken? break-sub pump
reset-all-ice update-all-ice update-all-icebreakers
update-breaker-strength]]
[game.core.installing :refer [runner-can-install? runner-can-pay-and-install? runner-install]]
[game.core.link :refer [get-link link+]]
[game.core.memory :refer [caissa-mu+ mu+ update-mu virus-mu+]]
[game.core.moving :refer [mill move swap-agendas trash trash-cards]]
[game.core.optional :refer [get-autoresolve never? set-autoresolve]]
[game.core.payment :refer [build-cost-string can-pay? cost-value]]
[game.core.play-instants :refer [play-instant]]
[game.core.prompts :refer [cancellable clear-wait-prompt]]
[game.core.props :refer [add-counter add-icon remove-icon]]
[game.core.revealing :refer [reveal]]
[game.core.rezzing :refer [derez rez]]
[game.core.runs :refer [bypass-ice end-run end-run-prevent
get-current-encounter jack-out make-run
successful-run-replace-breach total-cards-accessed]]
[game.core.say :refer [system-msg]]
[game.core.servers :refer [target-server is-central?]]
[game.core.shuffling :refer [shuffle!]]
[game.core.tags :refer [gain-tags lose-tags tag-prevent]]
[game.core.to-string :refer [card-str]]
[game.core.toasts :refer [toast]]
[game.core.update :refer [update!]]
[game.core.virus :refer [count-virus-programs number-of-virus-counters]]
[game.macros :refer [continue-ability effect msg req wait-for]]
[game.utils :refer :all]
[jinteki.utils :refer :all]
[game.core.set-aside :refer [set-aside get-set-aside]]
[game.core.sabotage :refer [sabotage-ability]]
[game.core.mark :refer [identify-mark-ability]]))
(defcard "Capstone"
{:abilities [{:req (req (pos? (count (:hand runner))))
:label "trash and install cards"
:cost [:click 1]
:async true
:prompt "Choose any number of cards to trash from the grip"
:choices {:max (req (count (:hand runner)))
:card #(and (runner? %)
(in-hand? %))}
:effect (req (let [trashed-card-names (keep :title targets)
installed-card-names (keep :title (all-active-installed state :runner))
overlap (set/intersection (set trashed-card-names)
(set installed-card-names))]
(wait-for (trash-cards state side targets {:unpreventable true
:cause-card card})
(let [trashed-cards async-result]
(wait-for (draw state side (count (filter overlap trashed-card-names)))
(system-msg state side
(str "uses " (:title card) " to trash "
(enumerate-str (map :title trashed-cards))
" from the grip and draw "
(quantify (count async-result) "card")))
(effect-completed state side eid))))))}]})
polyfy/polylith
(ns dev.jocke
(:require [dev.dev-common :as dev-common]
[clojure.set :as set]
[clojure.string :as str]
[clojure.pprint :as pp]
[clojure.tools.deps :as tools-deps]
[polylith.clj.core.api.interface :as api]
[polylith.clj.core.workspace.interface :as ws]
[polylith.clj.core.change.interface :as change]
[polylith.clj.core.command.interface :as command]
[polylith.clj.core.git.interface :as git]
[polylith.clj.core.util.interface :as util]
[polylith.clj.core.command.info :as info]
[polylith.clj.core.validator.m207-unnecessary-components-in-project :as validator207]
[polylith.clj.core.path-finder.interface.extract :as extract]
[polylith.clj.core.workspace-clj.interface :as ws-clj]
[polylith.clj.core.ws-explorer.interface :as ws-explorer]
[polylith.clj.core.common.interface :as common]
[polylith.clj.core.file.interface :as file]
[polylith.clj.core.help.interface :as help]
[clojure.tools.deps.util.maven :as mvn]
[polylith.clj.core.user-input.interface :as user-input])
(:refer-clojure :exclude [base]))
(def changed-components (-> workspace :changes :changed-components))
(def changed-bases (-> workspace :changes :changed-bases))
(def changed-bricks (set (concat changed-components changed-bases)))
(def brick-changed? (-> (set/intersection bricks changed-bricks)
empty? not))
borkdude/speculative
(ns speculative.set
(:require [clojure.set :as set]
[clojure.spec.alpha :as s]
[speculative.specs :as ss]))
(s/fdef set/intersection
:args (s/alt :unary ::unary
:binary ::binary
:variadic ::variadic)
:ret ::nilable-set)