clj-kondo

https://github.com/clj-kondo/clj-kondo
nivekuil 2021-01-17T15:56:33.026100Z

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

borkdude 2021-01-17T16:24:12.027Z

@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

nivekuil 2021-01-17T17:01:14.027200Z

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?

nivekuil 2021-01-17T17:07:22.027500Z

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 :)

borkdude 2021-01-17T17:07:42.027900Z

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

borkdude 2021-01-17T17:07:48.028100Z

same for union, etc

borkdude 2021-01-17T17:08:02.028400Z

so never use those fns with something else than sets