Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* src/main/clojure/clojure/core/match.clj: MATCH-10: support heteroge…

…nous keys in maps
  • Loading branch information...
commit 3a2e5598b6fc7ad52caa0869802d0ed80ba54124 1 parent 2349d38
@swannodette swannodette authored
View
9 src/main/clojure/clojure/core/match.clj
@@ -950,6 +950,11 @@
(declare map-pattern?)
(declare guard-pattern)
+(defn key-compare [a b]
+ (if (= (type a) (type b))
+ (compare a b)
+ -1))
+
(deftype MapPattern [m _meta]
clojure.lang.IObj
(meta [_] _meta)
@@ -978,7 +983,7 @@
(set only)])))
(reduce concat)
(reduce set/union #{})
- sort) ;; NOTE: this assumes keys are of a homogenous type, can't sort #{1 :a} - David
+ (sort key-compare)) ;; NOTE: this assumes keys are of a homogenous type, can't sort #{1 :a} - David
wcs (repeatedly wildcard-pattern)
wc-map (zipmap all-keys wcs)
nrows (->> rows
@@ -995,7 +1000,7 @@
[{} wc-map])]
(merge not-found-map wc-map m))
wc-map)
- ps (map second (sort ocr-map))
+ ps (map second (sort (fn [[a _] [b _]] (key-compare a b)) ocr-map))
ps (if @only?
(if only
(let [a (with-meta (gensym) {:tag 'java.util.Map})]
View
9 src/test/clojure/clojure/core/match/test/core.clj
@@ -469,6 +469,13 @@
:else :a1))
:a1))))
+(deftest map-pattern-heterogenous-keys-1
+ (is (= (let [m {:foo 1 "bar" 2}]
+ (match [m]
+ [{:foo 1 "bar" 2}] :a0
+ :else :a1))
+ :a0)))
+
(deftest exception-1
(is (= (try
(match-1 :a :a (throw (Exception.)) :else :c)
@@ -514,4 +521,4 @@
[([1 2] :seq) _] :a2
[_ 2] :a3
:else :a4))
- :a3)))
+ :a3)))
Please sign in to comment.
Something went wrong with that request. Please try again.