Skip to content

Commit

Permalink
* src/main/clojure/clojure/core/match.clj: MATCH-10: support heteroge…
Browse files Browse the repository at this point in the history
…nous keys in maps
  • Loading branch information
swannodette committed Oct 28, 2011
1 parent 2349d38 commit 3a2e559
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/main/clojure/clojure/core/match.clj
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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})]
Expand Down
9 changes: 8 additions & 1 deletion src/test/clojure/clojure/core/match/test/core.clj
Expand Up @@ -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)
Expand Down Expand Up @@ -514,4 +521,4 @@
[([1 2] :seq) _] :a2
[_ 2] :a3
:else :a4))
:a3)))
:a3)))

0 comments on commit 3a2e559

Please sign in to comment.