Skip to content

Commit

Permalink
* src/main/clojure/clojure/core/match/core.clj: Fixes Match-11: does …
Browse files Browse the repository at this point in the history
…mess w/ specified order. This ended up revealing another issue which is that guard patterns should get grouped w/ whatever pattern they actually contain.
  • Loading branch information
swannodette committed Sep 26, 2011
1 parent 64eb9d4 commit 5e2762f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
18 changes: 11 additions & 7 deletions src/main/clojure/clojure/core/match/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
(zero? (pattern-compare a b)))

(defmethod pattern-compare :default
[a b] (if (= (class a) (class b)) 0 -1))
[a b] (if (= (class a) (class b)) 0 1))

;; =============================================================================
;; # Pattern Rows
Expand Down Expand Up @@ -1205,7 +1205,7 @@
[a b] 0)

(defmethod pattern-compare [LiteralPattern Object]
[a b] -1)
[a b] 1)

(prefer-method pattern-compare [Object WildcardPattern] [LiteralPattern Object])

Expand All @@ -1218,19 +1218,23 @@
lb (.l b)]
(cond
(= la lb) 0
(symbol? la) 1
(symbol? lb) -1
:else (compare la lb))))
:else 1)))

(defmethod pattern-compare [GuardPattern GuardPattern]
[^GuardPattern a ^GuardPattern b] (if (= (.gs a) (.gs b)) 0 -1))
[^GuardPattern a ^GuardPattern b] (if (= (.gs a) (.gs b)) 0 1))

(defmethod pattern-compare [GuardPattern WildcardPattern]
[^GuardPattern a ^WildcardPattern b]
(let [p (.p a)]
(if (wildcard-pattern? p)
(pattern-compare p b) 1)))

(defmethod pattern-compare [OrPattern OrPattern]
[^OrPattern a ^OrPattern b] (let [as (.ps a)
bs (.ps b)]
(if (and (= (count as) (count bs))
(every? identity (map pattern-equals as bs)))
0 -1)))
0 1)))

;; TODO: vector pattern compare - David

Expand Down
18 changes: 11 additions & 7 deletions src/test/clojure/clojure/core/match/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,14 @@
:else :a2))
:a0)))

(deftest match-single-3
(is (= (match-1 [1 2]
[2 1] :a0
(_ :when #(= (count %) 2)) :a1
:else :a2)
:a1)))
(comment
(deftest match-single-3
(is (= (match-1 [1 2]
[2 1] :a0
(_ :when #(= (count %) 2)) :a1
:else :a2)
:a1)))
)

(deftest match-local-1
(is (= (let [x 2
Expand Down Expand Up @@ -359,4 +361,6 @@
(match [v]
[[]] 1
:else 2))
1)))
1)))

(apply sorted-set-by (fn [a b] (pattern-compare a b)) [(literal-pattern 2) (wildcard-pattern 'a)])

0 comments on commit 5e2762f

Please sign in to comment.