Welcome! Please see the About page for a little more info on how this works.

0 votes
in Collections by

clojure.set/intersection appears to use the indexes of vectors as values. This results in very strange behavior if you accidentally end up passing a vector in as one of the arguments.

`
ti.repl-init=> (clojure.set/intersection #{0 1} [2 2 2 2 2])

{0 1}

ti.repl-init=> (clojure.set/intersection [2 2 2 2] #{0 1})

{0 1}

ti.repl-init=> (clojure.set/intersection [0 1] [2 2 2 2])
[0 1]
ti.repl-init=> (clojure.set/intersection [2 2 2 2] [2 2 2 2])
[2 2 2 2]
ti.repl-init=> (clojure.set/intersection [3 3 3 ] [2 2 2 2])
[3 3 3]
ti.repl-init=> (clojure.set/intersection [55] [2 2 2 2])

ClassCastException clojure.lang.PersistentVector cannot be cast to clojure.lang.IPersistentSet clojure.core/disj (core.clj:1476)
`

If any of the arguments are lists, you get a ClassCastException which is maybe a bit less clear than one would hope.

`
ti.repl-init=> (clojure.set/intersection #{0 1} (list 2 2 2 2))

IllegalArgumentException contains? not supported on type: clojure.lang.PersistentList clojure.lang.RT.contains (RT.java:814)
`

The same also happens if all arguments are lists:

4 Answers

0 votes
by
_Comment made by: ashtonkemerling_

More odd side effects.


ti.repl-init=> (clojure.set/intersection #{:foo} {:foo 1})
#{:foo}
ti.repl-init=> (clojure.set/intersection #{:foo} {})
{}
ti.repl-init=> (clojure.set/intersection #{:foo} [:foo])
#{}
ti.repl-init=> (clojure.set/intersection [:foo] [:foo])

ClassCastException clojure.lang.PersistentVector cannot be cast to clojure.lang.IPersistentSet  clojure.core/disj (core.clj:1476)
ti.repl-init=> (clojure.set/intersection [0] [:foo])
[0]
0 votes
by

Comment made by: alexmiller

See comments on CLJ-1953

0 votes
by

Comment made by: jafingerhut

If you want an off-the-shelf compatible replacement for clojure.set functions that are identical in behavior, except they perform run-time type checks of the arguments you provide to them, and throw an exception if they have the wrong types (e.g. not sets for union, intersection, difference, subset?, and superset?), consider using the fungible library: https://github.com/jafingerhut/funjible

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1954 (reported by alex+import)
...