Skip to content
Browse files

* src/main/clojure/clojure/core/match/core.clj: Fixes MATCH-17: added…

… missing pattern-compare for VectorPattern
  • Loading branch information...
1 parent 60b84fa commit 1b01e7fc6634e081f6a96db41eb65c91b95b8161 @swannodette swannodette committed Sep 26, 2011
Showing with 21 additions and 9 deletions.
  1. +14 −8 src/main/clojure/clojure/core/match/core.clj
  2. +7 −1 src/test/clojure/clojure/core/match/test/core.clj
View
22 src/main/clojure/clojure/core/match/core.clj
@@ -1019,7 +1019,7 @@
size (if rest? (dec rvc) rvc)]
(VectorPattern. rv t size n rest? _meta)))]
[pl pr]))
- ISpecializeMatrix
+ ISpecializeMatrix
(specialize-matrix [this matrix]
(let [rows (rows matrix)
ocrs (occurrences matrix)
@@ -1230,13 +1230,19 @@
(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)))
-
-;; TODO: vector pattern compare - David
+ [^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)))
+
+(defmethod pattern-compare [VectorPattern VectorPattern]
+ [^VectorPattern a ^VectorPattern b]
+ (if (or (= (.size a) (.size b))
+ (and (.rest? a) (<= (.size a) (.size b)))
+ (and (.rest? b) (<= (.size b) (.size a))))
+ 0 1))
;; =============================================================================
;; # Interface
View
8 src/test/clojure/clojure/core/match/test/core.clj
@@ -365,4 +365,10 @@
:else 2))
1)))
-(apply sorted-set-by (fn [a b] (pattern-compare a b)) [(literal-pattern 2) (wildcard-pattern 'a)])
+(deftest empty-vector-2
+ (is (= (let [v [1 2]]
+ (match [v]
+ [[]] :a0
+ [[x & r]] :a1
+ :else :a3))
+ :a1)))

0 comments on commit 1b01e7f

Please sign in to comment.
Something went wrong with that request. Please try again.