Skip to content

Commit

Permalink
CLJ-1831 Add map-entry? predicate
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@cognitect.com>
  • Loading branch information
puredanger authored and stuarthalloway committed Oct 27, 2015
1 parent 15932d8 commit 5da21b3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/clj/clojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,15 @@

;;map stuff

(defn map-entry?
"Return true if x is a map entry"
{:added "1.8"}
[x]
(and (instance? java.util.Map$Entry x)
(if (instance? clojure.lang.IPersistentVector x)
(= 2 (count x))
true)))

(defn contains?
"Returns true if key is present in the given collection, otherwise
returns false. Note that for numerically indexed collections like
Expand Down
2 changes: 1 addition & 1 deletion src/clj/clojure/core_deftype.clj
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
(defn- imap-cons
[^IPersistentMap this o]
(cond
(instance? java.util.Map$Entry o)
(map-entry? o)
(let [^java.util.Map$Entry pair o]
(.assoc this (.getKey pair) (.getValue pair)))
(instance? clojure.lang.IPersistentVector o)
Expand Down
2 changes: 1 addition & 1 deletion src/clj/clojure/inspector.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

(defn collection-tag [x]
(cond
(instance? java.util.Map$Entry x) :entry
(map-entry? x) :entry
(instance? java.util.Map x) :seqable
(instance? java.util.Set x) :seqable
(sequential? x) :seq
Expand Down
10 changes: 10 additions & 0 deletions test/clojure/test_clojure/data_structures.clj
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,16 @@
ai3 ao3
ai4 ao4)))

(deftest test-map-entry?
(testing "map-entry? = false"
(are [entry]
(false? (map-entry? entry))
nil 5 #{1 2} '(1 2) {:a 1} [] [0] [1 2 3]))
(testing "map-entry? = true"
(are [entry]
(true? (map-entry? entry))
[1 2] (first (doto (java.util.HashMap.) (.put "x" 1))))))

;; *** Sets ***

(deftest test-hash-set
Expand Down

0 comments on commit 5da21b3

Please sign in to comment.