Skip to content

Commit 5da21b3

Browse files
puredangerstuarthalloway
authored andcommitted
CLJ-1831 Add map-entry? predicate
Signed-off-by: Stuart Halloway <stu@cognitect.com>
1 parent 15932d8 commit 5da21b3

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

src/clj/clojure/core.clj

+9
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,15 @@
14251425

14261426
;;map stuff
14271427

1428+
(defn map-entry?
1429+
"Return true if x is a map entry"
1430+
{:added "1.8"}
1431+
[x]
1432+
(and (instance? java.util.Map$Entry x)
1433+
(if (instance? clojure.lang.IPersistentVector x)
1434+
(= 2 (count x))
1435+
true)))
1436+
14281437
(defn contains?
14291438
"Returns true if key is present in the given collection, otherwise
14301439
returns false. Note that for numerically indexed collections like

src/clj/clojure/core_deftype.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
(defn- imap-cons
133133
[^IPersistentMap this o]
134134
(cond
135-
(instance? java.util.Map$Entry o)
135+
(map-entry? o)
136136
(let [^java.util.Map$Entry pair o]
137137
(.assoc this (.getKey pair) (.getValue pair)))
138138
(instance? clojure.lang.IPersistentVector o)

src/clj/clojure/inspector.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
(defn collection-tag [x]
2323
(cond
24-
(instance? java.util.Map$Entry x) :entry
24+
(map-entry? x) :entry
2525
(instance? java.util.Map x) :seqable
2626
(instance? java.util.Set x) :seqable
2727
(sequential? x) :seq

test/clojure/test_clojure/data_structures.clj

+10
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,16 @@
704704
ai3 ao3
705705
ai4 ao4)))
706706

707+
(deftest test-map-entry?
708+
(testing "map-entry? = false"
709+
(are [entry]
710+
(false? (map-entry? entry))
711+
nil 5 #{1 2} '(1 2) {:a 1} [] [0] [1 2 3]))
712+
(testing "map-entry? = true"
713+
(are [entry]
714+
(true? (map-entry? entry))
715+
[1 2] (first (doto (java.util.HashMap.) (.put "x" 1))))))
716+
707717
;; *** Sets ***
708718

709719
(deftest test-hash-set

0 commit comments

Comments
 (0)