Fork me on GitHub
#clj-kondo
<
2021-01-17
>
nivekuil15:01:33

clj-kondo flags this: (set/difference #{:a :b} (keys {:a 1})) but it actually does work. That seq from keys can be used like a set

borkdude16:01:12

@kevin842 this is undeterministic behavior.

(instance? java.util.Set (keys {:a 1}))
false
E.g. this will crash:
user=> (set/difference #{:a :b} (keys {:a 1 :b 2 :c 3}))
Execution error (IllegalArgumentException) at user/eval7 (REPL:1).
contains? not supported on type: clojure.lang.APersistentMap$KeySeq

nivekuil17:01:14

oh, interesting! I wonder why that is the case.. isn't it just a view over the actual collection, which it knows to support o(1) access?

nivekuil17:01:22

I must be thinking of python, where dict.keys() does return a set-like view:

>>> "b" in {"a": 1, "b": 2}.keys() True
in any case not relevant to this channel :)

borkdude17:01:42

@kevin842 this happens because set/difference checks which input is larger and uses that to make things faster

borkdude17:01:48

same for union, etc

borkdude17:01:02

so never use those fns with something else than sets