How to distinguish java card from clojure card?

Let's say I have the following code:

(def m1 (java.util.HashMap.))
(def m2 (java.util.LinkedHashMap.))
(def m3 {})

I need a function that will allow me to detect maps that come from java, for example:

(map java-map? [m1 m2 m3]) ;; => (true true false)

Anything out of the box?

+4
source share
2 answers

I would do this:

user=> (defn java-map? [m] 
         (and (instance? java.util.Map m) 
              (not (map? m))))
#'user/java-map?

user=> (java-map? {})
false

user=> (java-map? (java.util.HashMap.))
true

user=> (java-map? [])
false

therefore, you just verify that it implements the java kernel interface Map, but is not a stable clojure map.

+4
source

You can use map?to verify that something implements IPersistentMapthat is true for Clojure cards, but not for cards java.utils.*:

(map? (java.util.HashMap.)) ;; => false
(map? (java.util.LinkedHashMap.)) ;; => false
(map? {}) ;; => true

, , (, , / - map? ). , Java- , , java.util.Map java.util.

+8

All Articles