[Git][clojure-team/core-match-clojure][upstream] New upstream version 1.0.0
Louis-Philippe Véronneau
gitlab at salsa.debian.org
Mon Nov 30 23:53:50 GMT 2020
Louis-Philippe Véronneau pushed to branch upstream at Debian Clojure Maintainers / core-match-clojure
Commits:
a751e8de by Louis-Philippe Véronneau at 2020-11-30T16:38:44-05:00
New upstream version 1.0.0
- - - - -
19 changed files:
- + .github/PULL_REQUEST_TEMPLATE
- .gitignore
- CHANGES.md
- CONTRIBUTING.md
- README.md
- + deps.edn
- pom.xml
- project.clj
- src/main/clojure/cljs/core/match/macros.clj → src/main/clojure/cljs/core/match.clj
- src/main/clojure/cljs/core/match.cljs
- src/main/clojure/clojure/core/match.clj
- src/main/clojure/clojure/core/match/array.clj
- src/main/clojure/clojure/core/match/protocols.clj
- src/main/clojure/clojure/core/match/regex.clj
- src/test/cljs/core/match/macro_test.clj
- src/test/cljs/core/match/tests.cljs
- src/test/clojure/clojure/core/match/test/core.clj
- src/test/clojure/clojure/core/match/test/date.clj
- src/test/clojure/clojure/core/match/test/java.clj
Changes:
=====================================
.github/PULL_REQUEST_TEMPLATE
=====================================
@@ -0,0 +1,14 @@
+Hi! Thanks for your interest in contributing to this project.
+
+Clojure contrib projects do not use GitHub issues or pull requests, and
+require a signed Contributor Agreement. If you would like to contribute,
+please read more about the CA and sign that first (this can be done online).
+
+Then go to this project's issue tracker in JIRA to create tickets, update
+tickets, or submit patches. For help in creating tickets and patches,
+please see:
+
+- Signing the CA: https://clojure.org/community/contributing
+- Creating Tickets: https://clojure.org/community/creating_tickets
+- Developing Patches: https://clojure.org/community/developing_patches
+- Contributing FAQ: https://clojure.org/community/contributing
=====================================
.gitignore
=====================================
@@ -1,3 +1,6 @@
+.idea
*jar
/target/
-.lein-failures
\ No newline at end of file
+.lein-failures
+.nrepl-port
+.cpcache/
=====================================
CHANGES.md
=====================================
@@ -1,3 +1,65 @@
+From 0.3.0-alpha5 to 0.3.0
+===
+
+Fixes
+---
+* MATCH-124 - correct namespaced keywords to work with Clojure 1.9+
+
+From 0.3.0-alpha4 to 0.3.0-alpha5
+===
+
+Fixes
+---
+* Remove unused tools.analyzer dependency
+
+From 0.3.0-alpha3 to 0.3.0-alpha4
+===
+
+Fixes
+---
+* Add `matchm` to ClojureScript support
+
+From 0.3.0-alpha2 to 0.3.0-alpha3
+===
+
+Fixes
+---
+* Fix regression in test sharing due to unfinished literal case work
+
+From 0.3.0-alpha1 to 0.3.0-alpha2
+===
+
+Fixes
+---
+* Fix fn app pattern issue when nested in maps & vectors
+
+===
+
+From 0.2.2 to 0.3.0-alpha1
+===
+
+Changes
+---
+* Depend on tools.analyzer
+* MATCH-100: Support function application in match syntax
+* MATCH-97: satisfies? calls for IMatchLookup perf issues, breaking
+
+Fixes
+---
+* MATCH-98: RegexPattern cannot be aot-compiled
+
+From 0.2.1 to 0.2.2
+===
+
+Changes
+---
+* Depend on tools.analyzer
+
+Fixes
+---
+* MATCH-86: Cannot AOT OrPatterns
+* sort pattern duplicates to avoid order issues now that hash values have changed in Clojure 1.6.0
+
From 0.2.0 to 0.2.1
===
=====================================
CONTRIBUTING.md
=====================================
@@ -3,12 +3,10 @@ This is a [Clojure contrib] project.
Under the Clojure contrib [guidelines], this project cannot accept
pull requests. All patches must be submitted via [JIRA].
-See [Contributing] and the [FAQ] on the Clojure development [wiki] for
+See [Contributing] on the Clojure website for
more information on how to contribute.
-[Clojure contrib]: http://dev.clojure.org/display/doc/Clojure+Contrib
-[Contributing]: http://dev.clojure.org/display/community/Contributing
-[FAQ]: http://dev.clojure.org/display/community/Contributing+FAQ
+[Clojure contrib]: https://clojure.org/community/contrib_libs
+[Contributing]: https://clojure.org/community/contributing
[JIRA]: http://dev.clojure.org/jira/browse/MATCH
-[guidelines]: http://dev.clojure.org/display/community/Guidelines+for+Clojure+Contrib+committers
-[wiki]: http://dev.clojure.org/
+[guidelines]: https://clojure.org/community/contrib_howto
=====================================
README.md
=====================================
@@ -1,21 +1,23 @@
match
====
-An optimized pattern match and predicate dispatch library for Clojure. Currently the library only implements pattern matching. It supports Clojure 1.3.0 and later as well as ClojureScript.
+An optimized pattern matching library for Clojure. It supports Clojure
+1.5.1 and later as well as ClojureScript.
-You can find more detailed information [here](https://github.com/clojure/core.match/wiki/Overview).
+You can find more detailed information
+[here](https://github.com/clojure/core.match/wiki/Overview).
Releases and dependency information
----
-Latest beta: 0.2.1
+Latest release: 0.3.0
* [All released versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22core.match%22)
[Leiningen](http://github.com/technomancy/leiningen/) dependency information:
```
-[org.clojure/core.match "0.2.1"]
+[org.clojure/core.match "0.3.0"]
```
[Maven](http://maven.apache.org) dependency information:
@@ -24,7 +26,7 @@ Latest beta: 0.2.1
<dependency>
<groupId>org.clojure</groupId>
<artifactId>core.match</artifactId>
- <version>0.2.1</version>
+ <version>0.3.0</version>
</dependency>
```
@@ -34,7 +36,7 @@ Example Usage
From Clojure:
```clojure
-(use '[clojure.core.match :only (match)])
+(require '[clojure.core.match :refer [match]])
(doseq [n (range 1 101)]
(println
@@ -49,8 +51,7 @@ From ClojureScript:
```clojure
(ns foo.bar
- (:require-macros [cljs.core.match.macros :refer [match]])
- (:require [cljs.core.match]))
+ (:require [cljs.core.match :refer-macros [match]]))
(doseq [n (range 1 101)]
(println
@@ -74,6 +75,7 @@ Developer information
Copyright and license
----
-Copyright © 2010-2013 David Nolen, Rich Hickey & contributors.
+Copyright © 2010-2019 David Nolen, Ambrose Bonnaire-Sergeant, Rich
+Hickey & contributors.
Licensed under the EPL (see the file epl.html).
=====================================
deps.edn
=====================================
@@ -0,0 +1 @@
+{:paths ["src/main/clojure"]}
=====================================
pom.xml
=====================================
@@ -2,8 +2,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core.match</artifactId>
- <version>0.2.2</version>
- <name>${artifactId}</name>
+ <version>1.0.0</version>
+ <name>core.match</name>
<description>Optimized pattern matching and predicate dispatch for Clojure</description>
<licenses>
@@ -17,7 +17,7 @@
<parent>
<groupId>org.clojure</groupId>
<artifactId>pom.contrib</artifactId>
- <version>0.1.2</version>
+ <version>0.2.2</version>
</parent>
<issueManagement>
@@ -44,14 +44,14 @@
<dependencies>
<dependency>
<groupId>org.clojure</groupId>
- <artifactId>clojurescript</artifactId>
- <version>0.0-2311</version>
- <scope>provided</scope>
+ <artifactId>clojure</artifactId>
+ <version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.clojure</groupId>
- <artifactId>tools.analyzer.jvm</artifactId>
- <version>0.1.0-beta12</version>
+ <artifactId>clojurescript</artifactId>
+ <version>0.0-2411</version>
+ <scope>provided</scope>
</dependency>
</dependencies>
@@ -66,6 +66,6 @@
<connection>scm:git:git://github.com/clojure/core.match.git</connection>
<developerConnection>scm:git:git://github.com/clojure/core.match.git</developerConnection>
<url>http://github.com/clojure/core.match</url>
- <tag>core.match-0.2.2</tag>
+ <tag>core.match-1.0.0</tag>
</scm>
</project>
=====================================
project.clj
=====================================
@@ -1,5 +1,5 @@
-(defproject org.clojure/core.match "0.2.2-SNAPSHOT"
- :description "Optimized pattern matching and predicate dispatch for Clojure"
+(defproject org.clojure/core.match "0.3.0-alpha4-SNAPSHOT"
+ :description "Optimized pattern matching for Clojure"
:jvm-opts ^:replace ["-Xmx512m" "-server"]
@@ -7,12 +7,10 @@
:source-paths ["src/main/clojure"]
:dependencies [[org.clojure/clojure "1.6.0"]
- [org.clojure/tools.analyzer.jvm "0.1.0-beta12"]
- [org.clojure/clojurescript "0.0-2311" :scope "provided"]]
+ [org.clojure/clojurescript "0.0-2496" :scope "provided"]]
- :dev-dependencies [[org.clojure/tools.nrepl "0.2.3"]]
-
- :plugins [[lein-cljsbuild "1.0.4-SNAPSHOT"]]
+ :plugins [[lein-cljsbuild "1.0.4-SNAPSHOT"]
+ [cider/cider-nrepl "0.8.1"]]
:cljsbuild
{:builds
=====================================
src/main/clojure/cljs/core/match/macros.clj → src/main/clojure/cljs/core/match.clj
=====================================
@@ -1,6 +1,6 @@
-(ns cljs.core.match.macros
+(ns cljs.core.match
(:refer-clojure :exclude [compile])
- (:use [clojure.core.match :exclude [match matchv match-let]]))
+ (:use [clojure.core.match :exclude [match matchv match-let matchm]]))
(defmacro asets [a vs]
`(do
@@ -47,6 +47,7 @@
*warned* (atom false)]
`~(clj-form vars clauses)))
+
(defmacro matchv* [type vars & clauses]
(binding [*clojurescript* true
*vector-type* type
@@ -56,6 +57,21 @@
*no-backtrack* true]
`~(clj-form vars clauses)))
+(defmacro matchm
+ [vars & clauses]
+ (let [[vars clauses]
+ (if (vector? vars)
+ [vars clauses]
+ [(vector vars)
+ (mapcat (fn [[c a]]
+ [(if (not= c :else) (vector c) c) a])
+ (partition 2 clauses))])]
+ (binding [*clojurescript* true
+ *line* (-> &form meta :line)
+ *locals* (dissoc (:locals &env) '_)
+ *warned* (atom false)]
+ `~(clj-form vars clauses))))
+
(defmacro match-let [bindings & body]
(let [bindvars# (take-nth 2 bindings)]
`(let ~bindings
=====================================
src/main/clojure/cljs/core/match.cljs
=====================================
@@ -1,3 +1,4 @@
-(ns cljs.core.match)
+(ns cljs.core.match
+ (:require-macros cljs.core.match))
(def backtrack (js/Error.))
=====================================
src/main/clojure/clojure/core/match.clj
=====================================
@@ -1,10 +1,7 @@
(ns clojure.core.match
(:refer-clojure :exclude [compile])
(:use [clojure.core.match.protocols])
- (:require [clojure.set :as set]
- [clojure.tools.analyzer :as ana]
- [clojure.tools.analyzer.jvm :as ana-jvm]
- [clojure.tools.analyzer.passes.jvm.annotate-loops :as loops])
+ (:require [clojure.set :as set])
(:import [java.io Writer]
[clojure.core.match.protocols IExistentialPattern IPseudoPattern]))
@@ -57,6 +54,10 @@
(def ^{:dynamic true} *locals* nil)
(def ^{:dynamic true} *warned*)
+(def ^{:dynamic true
+ :doc "Allow map matching syntax to check for IMatchLookup"}
+ *match-lookup* false)
+
(def ^{:dynamic true
:doc "Default vector type. Can be rebound allowing emission of
custom inline code for vector patterns, for example
@@ -95,13 +96,6 @@
msg))
(reset! *warned* true)))
-(defn analyze [form env]
- (binding [ana/macroexpand-1 ana-jvm/macroexpand-1
- ana/create-var ana-jvm/create-var
- ana/parse ana-jvm/parse
- ana/var? var?]
- (ana/analyze form env)))
-
(defn get-loop-locals []
(let [LOOP_LOCALS clojure.lang.Compiler/LOOP_LOCALS]
(mapcat
@@ -245,7 +239,7 @@
(defmulti groupable?
"Determine if two patterns may be grouped together for simultaneous
testing."
- (fn [a b] [(type a) (type b)]))
+ (fn [a b] [(::tag a) (::tag b)]))
(defmethod groupable? :default
[a b] (= a b))
@@ -730,24 +724,35 @@
bs (row-bindings f ocrs)]
(leaf-node a bs)))
+;; if the first pattern in the first column is a
+;; pseudo-pattern, expand until it isn't, looking at
+;; any rows beyond the first causes problems for
+;; fn application pattern
+;; TODO: col is always ZERO - this is confusing
+;; that it takes col as an argument, fix - David
+
(defn expand-matrix [matrix col]
- (reduce
- (fn [matrix p]
- (specialize matrix p))
- matrix (pseudo-patterns matrix col)))
+ (loop [matrix matrix]
+ (let [p (first (column matrix col))]
+ (if (pseudo-pattern? p)
+ (recur (specialize matrix p))
+ matrix))))
(defn split-matrix [matrix]
- (if (non-local-literal-pattern? (ffirst (rows matrix)))
+ (matrix-splitter matrix)
+ #_(if (non-local-literal-pattern? (ffirst (rows matrix)))
;; literal testing based on equality can do w/o
;; backtracking for all adjacent literal ctors in a column
(literal-case-matrix-splitter matrix)
- (matrix-splitter matrix)))
+ (matrix-splitter matrix))
+ )
(defn first-column-chosen-case
"Case 3a: The first column is chosen. Compute and return a
switch/bind node with a default matrix case"
[matrix col ocrs]
(let [expanded (expand-matrix matrix col)
+ ocrs (occurrences expanded)
[S D :as split] (split-matrix expanded)]
(if-not *recur-present*
(switch-node (ocrs col)
@@ -911,6 +916,7 @@ col with the first column and compile the result"
(valAt [this k not-found]
(case k
:l l
+ ::tag ::literal
not-found))
IPatternCompile
@@ -1009,6 +1015,7 @@ col with the first column and compile the result"
(valAt [this k not-found]
(case k
:s s
+ ::tag ::seq
not-found))
IPatternCompile
@@ -1052,7 +1059,7 @@ col with the first column and compile the result"
(defrecord RestPattern [p])
(defn rest-pattern [p]
- (RestPattern. p))
+ (assoc (RestPattern. p) ::tag ::rest))
(defn rest-pattern? [x]
(instance? RestPattern x))
@@ -1088,7 +1095,7 @@ col with the first column and compile the result"
(pattern-matrix nrows ocrs))))
(defn map-key-pattern [p]
- (MapKeyPattern. p))
+ (assoc (MapKeyPattern. p) ::tag ::map-key))
(defn map-key-pattern? [x]
(instance? MapKeyPattern x))
@@ -1193,13 +1200,15 @@ col with the first column and compile the result"
(valAt [this k not-found]
(case k
:m m
+ ::tag ::map
not-found))
IPatternCompile
(to-source* [this ocr]
- (if *clojurescript*
- `(satisfies? cljs.core/ILookup ~ocr)
- `(or (instance? clojure.lang.ILookup ~ocr) (satisfies? IMatchLookup ~ocr))))
+ (cond
+ *clojurescript* `(satisfies? cljs.core/ILookup ~ocr)
+ *match-lookup* `(or (instance? clojure.lang.ILookup ~ocr) (satisfies? IMatchLookup ~ocr))
+ :else `(instance? clojure.lang.ILookup ~ocr)))
ISpecializeMatrix
(specialize-matrix [this matrix]
@@ -1336,6 +1345,7 @@ col with the first column and compile the result"
:size size
:offset offset
:rest? rest?
+ ::tag ::vector
not-found))
IPatternCompile
@@ -1430,6 +1440,7 @@ col with the first column and compile the result"
(valAt [this k not-found]
(case k
:ps ps
+ ::tag ::or
not-found))
ISpecializeMatrix
@@ -1487,6 +1498,7 @@ col with the first column and compile the result"
(case k
:p p
:gs gs
+ ::tag ::guard
not-found))
IPatternCompile
@@ -1512,6 +1524,89 @@ col with the first column and compile the result"
(defmethod print-method GuardPattern [p ^Writer writer]
(.write writer (str "<GuardPattern " (:p p) " :guard " (:gs p) ">")))
+;; -----------------------------------------------------------------------------
+;; ## Function Application Pattern
+;;
+;; Function Application patterns are used to represent function application on
+;; occurrences. Pattern matching will continue on the result of the application.
+;; `(3 :<< inc)`
+
+(declare app-pattern?)
+
+;; take the original occurence and replace it with a wildcard in each
+;; row that has a compatible application create & new occurrence whose
+;; binding is the old occurence with the function applied
+
+(defn app-pattern-matrix-ocrs [[focr :as ocrs] form]
+ (into
+ [(with-meta
+ (gensym (str "app_" focr))
+ {:bind-expr `(~form ~(or (-> focr meta :bind-expr) focr))})]
+ ocrs))
+
+(defn specialize-app-pattern-matrix [rows form]
+ (let [[matched-rows rest-rows]
+ (split-with
+ (fn [[pat :as row]]
+ (and (app-pattern? pat)
+ (= (:form pat) form)))
+ rows)]
+ (vec
+ (concat
+ (map
+ (fn [row]
+ (prepend
+ (update-pattern row 0 (wildcard-pattern))
+ (:p (first row))))
+ matched-rows)
+ (map
+ (fn [row]
+ (prepend row (wildcard-pattern)))
+ rest-rows)))))
+
+(deftype AppPattern [p form _meta]
+ IPseudoPattern
+
+ Object
+ (toString [this]
+ (str p " :<< " form))
+ (equals [_ other]
+ (and (instance? AppPattern other)
+ (= p (:p other))
+ (= form (:form other))))
+
+ clojure.lang.IObj
+ (meta [_] _meta)
+ (withMeta [_ new-meta]
+ (AppPattern. p form new-meta))
+
+ clojure.lang.ILookup
+ (valAt [this k]
+ (.valAt this k nil))
+ (valAt [this k not-found]
+ (case k
+ :p p
+ :form form
+ ::tag ::app
+ not-found))
+
+ ISpecializeMatrix
+ (specialize-matrix [this matrix]
+ (let [rows (rows matrix)
+ ocrs (occurrences matrix)
+ nocrs (app-pattern-matrix-ocrs ocrs form)
+ nrows (specialize-app-pattern-matrix rows form)]
+ (pattern-matrix nrows nocrs))))
+
+(defn app-pattern [p form]
+ (AppPattern. p form nil))
+
+(defn app-pattern? [x]
+ (instance? AppPattern x))
+
+(defmethod print-method AppPattern [p ^Writer writer]
+ (.write writer (str "<AppPattern " (:p p) " :app " (:form p) ">")))
+
;; -----------------------------------------------------------------------------
;; ## Predicate Patterns
;;
@@ -1567,6 +1662,7 @@ col with the first column and compile the result"
(case k
:p p
:gs gs
+ ::tag ::predicate
not-found))
IPatternCompile
@@ -1595,31 +1691,35 @@ col with the first column and compile the result"
;; -----------------------------------------------------------------------------
;; Pattern Comparisons
-(defmethod groupable? [LiteralPattern LiteralPattern]
+(defmethod groupable? [::literal ::literal]
[a b] (= (:l a) (:l b)))
-(defmethod groupable? [GuardPattern GuardPattern]
+(defmethod groupable? [::guard ::guard]
[a b] (= (:gs a) (:gs b)))
-(defmethod groupable? [PredicatePattern PredicatePattern]
+(defmethod groupable? [::predice ::predicate]
[a b] (= (:gs a) (:gs b)))
-(defmethod groupable? [MapPattern MapPattern]
+(defmethod groupable? [::map ::map]
[a b]
(= (-> a meta :only) (-> b meta :only)))
-(defmethod groupable? [OrPattern OrPattern]
+(defmethod groupable? [::or ::or]
[a b]
(let [as (:ps a)
bs (:ps b)]
(and (= (count as) (count bs))
(every? identity (map groupable? as bs)))))
-(defmethod groupable? [VectorPattern VectorPattern]
+(defmethod groupable? [::vector ::vector]
[a b]
(and (= (:rest? a) (:rest? b))
(= (:size a) (:size b))))
+(defmethod groupable? [::app ::app]
+ [a b]
+ (and (= (:form a) (:form b))))
+
;; =============================================================================
;; # Interface
@@ -1628,13 +1728,28 @@ col with the first column and compile the result"
pattern matches the occurrence. Dispatches on the `type` of the
pattern. For instance, a literal pattern might return `(= ~(:pattern
pattern) ~ocr)`, using `=` to test for a match."
- (fn [pattern ocr] (type pattern)))
+ (fn [pattern ocr] (::tag pattern)))
(defmulti emit-pattern
"Returns the corresponding pattern for the given syntax. Dispatches
on the class of its argument. For example, `[(:or 1 2) 2]` is dispatched
as clojure.lang.IPersistentVector"
- class)
+ (fn [pattern] (syntax-tag pattern)))
+
+(extend-protocol ISyntaxTag
+ clojure.lang.IPersistentVector
+ (syntax-tag [_] ::vector)
+ clojure.lang.ISeq
+ (syntax-tag [_] ::seq)
+ clojure.lang.IPersistentMap
+ (syntax-tag [_] ::map)
+ clojure.lang.Symbol
+ (syntax-tag [_] ::symbol)
+
+ Object
+ (syntax-tag [_] :default)
+ nil
+ (syntax-tag [_] :default))
;; ============================================================================
;; # emit-pattern Methods
@@ -1656,12 +1771,12 @@ col with the first column and compile the result"
:else
(recur (next ps) t (conj v (emit-pattern (first ps)))))))))
-(defmethod emit-pattern clojure.lang.IPersistentVector
+(defmethod emit-pattern ::vector
[pat]
(let [ps (emit-patterns pat :vector)]
(vector-pattern ps *vector-type* 0 (some rest-pattern? ps))))
-(defmethod emit-pattern clojure.lang.IPersistentMap
+(defmethod emit-pattern ::map
[pat]
(map-pattern
(->> pat
@@ -1670,7 +1785,7 @@ col with the first column and compile the result"
(remove nil?)
(into {}))))
-(defmethod emit-pattern clojure.lang.Symbol
+(defmethod emit-pattern ::symbol
[pat]
(if (not= (get *locals* pat ::not-found) ::not-found)
(literal-pattern (with-meta pat (assoc (meta pat) :local true)))
@@ -1683,7 +1798,7 @@ col with the first column and compile the result"
(declare emit-pattern-for-syntax or-pattern as-pattern guard-pattern
predicate-pattern vector-pattern)
-(defmethod emit-pattern clojure.lang.ISeq
+(defmethod emit-pattern ::seq
[pat] (if (and (= (count pat) 2)
(= (first pat) 'quote)
(or (symbol? (second pat))
@@ -1709,6 +1824,9 @@ col with the first column and compile the result"
(defmethod emit-pattern-for-syntax [:default :as]
[[p _ sym]] (with-meta (emit-pattern p) {:as sym}))
+(defmethod emit-pattern-for-syntax [:default :<<]
+ [[p _ form]] (app-pattern (emit-pattern p) form))
+
(defmethod emit-pattern-for-syntax [:default :when]
[[p _ gs]]
(let [gs (if (not (vector? gs)) [gs] gs)]
@@ -2004,9 +2122,25 @@ col with the first column and compile the result"
*warned* (atom false)]
`~(clj-form vars clauses)))
+(defmacro matchm
+ "Same as match but supports IMatchLookup when
+ matching maps."
+ [vars & clauses]
+ (let [[vars clauses]
+ (if (vector? vars)
+ [vars clauses]
+ [(vector vars)
+ (mapcat (fn [[c a]]
+ [(if (not= c :else) (vector c) c) a])
+ (partition 2 clauses))])]
+ (binding [*match-lookup* true
+ *line* (-> &form meta :line)
+ *locals* (dissoc &env '_)
+ *warned* (atom false)]
+ `~(clj-form vars clauses))))
+
(defmacro match-let [bindings & body]
(let [bindvars# (take-nth 2 bindings)]
`(let ~bindings
(match [~@bindvars#]
~@body))))
-
=====================================
src/main/clojure/clojure/core/match/array.clj
=====================================
@@ -1,5 +1,4 @@
;; WARNING: this namespace is experimental
-
(ns ^{:skip-wiki true}
clojure.core.match.array
(:refer-clojure :exclude [compile])
=====================================
src/main/clojure/clojure/core/match/protocols.clj
=====================================
@@ -31,6 +31,9 @@
(defprotocol IVectorPattern
(split [this n]))
+(defprotocol ISyntaxTag
+ (syntax-tag [this]))
+
;; markers
(definterface IExistentialPattern)
=====================================
src/main/clojure/clojure/core/match/regex.clj
=====================================
@@ -1,17 +1,25 @@
(ns clojure.core.match.regex
- (:use [clojure.core.match
- :only [emit-pattern to-source groupable?]])
- (:import java.util.regex.Pattern))
+ (:require
+ [clojure.core.match.protocols :as mp]
+ [clojure.core.match :as m :refer [emit-pattern to-source groupable?]])
+ (:import [java.util.regex Pattern]))
;; # Regular Expression Extension
;;
;; This extension adds support for Clojure's regular expression syntax.
+(extend-type Pattern
+ mp/ISyntaxTag
+ (syntax-tag [_] ::m/regex))
+
(defrecord RegexPattern [regex])
-(defmethod emit-pattern java.util.regex.Pattern
+(defn regex-pattern [pat]
+ (assoc (RegexPattern. pat) ::m/tag ::m/regex))
+
+(defmethod emit-pattern ::m/regex
[pat]
- (RegexPattern. pat))
+ (regex-pattern pat))
;; Regular expressions are matched with `re-matches`.
;;
@@ -20,7 +28,7 @@
;;
;; `(re-matches #"olive" q)`
-(defmethod to-source RegexPattern
+(defmethod to-source ::m/regex
[pat ocr]
`(re-matches ~(:regex pat) ~ocr))
@@ -28,7 +36,7 @@
;;
;; Two `Pattern`s are equal if they have the same pattern and the same flags.
-(defmethod groupable? [RegexPattern RegexPattern]
+(defmethod groupable? [::m/regex ::m/regex]
[a b]
(let [^Pattern ra (:regex a)
^Pattern rb (:regex b)]
=====================================
src/test/cljs/core/match/macro_test.clj
=====================================
@@ -2,4 +2,4 @@
(defmacro test1 []
`(fn [& args#]
- (cljs.core.match.macros/match [1 2 3] [1 2 3] :match)))
+ (cljs.core.match/match [1 2 3] [1 2 3] :match)))
=====================================
src/test/cljs/core/match/tests.cljs
=====================================
@@ -2,9 +2,11 @@
(:require-macros
[clojure.core.match :as m]
[clojure.core.match.array]
- [cljs.core.match.macros :refer [match match* matchv matchv* asets]]
[cljs.core.match.macro-test :as mt])
- (:require [cljs.core.match]))
+ (:require [cljs.test :as test
+ :refer-macros [deftest testing is run-tests]]
+ [cljs.core.match
+ :refer-macros [match match* matchv matchv* asets]]))
(defn js-print [& args]
(if (js* "typeof console != 'undefined'")
@@ -16,261 +18,253 @@
;; =============================================================================
;; Basic matching
-(assert
- (= (let [x true
- y true
- z true]
- (match [x y z]
- [_ false true] 1
- [false true _ ] 2
- [_ _ false] 3
- [_ _ true] 4
- :else 5))
- 4))
-
-(assert
- (= ((fn [x y z done]
- (if (not done)
- (match [x y z]
- [_ false true] (recur x y z 1)
- [false true _ ] (recur x y z 2)
- [_ _ false] (recur x y z 3)
- [_ _ true] (recur x y z 4)
- :else 5)
- done)) true true true false)
- 4))
-
-(assert
- (= (let [x 1 y 2 z 4]
- (match [x y z]
- [1 2 b] [:a0 b]
- [a 2 4] [:a1 a]
- :else []))
- [:a0 4]))
+(deftest test-basics
+ (testing "Basics"
+ (is
+ (= (let [x true
+ y true
+ z true]
+ (match [x y z]
+ [_ false true] 1
+ [false true _ ] 2
+ [_ _ false] 3
+ [_ _ true] 4
+ :else 5))
+ 4))
+ (is
+ (= ((fn [x y z done]
+ (if (not done)
+ (match [x y z]
+ [_ false true] (recur x y z 1)
+ [false true _ ] (recur x y z 2)
+ [_ _ false] (recur x y z 3)
+ [_ _ true] (recur x y z 4)
+ :else 5)
+ done)) true true true false)
+ 4))
+ (is
+ (= (let [x 1 y 2 z 4]
+ (match [x y z]
+ [1 2 b] [:a0 b]
+ [a 2 4] [:a1 a]
+ :else []))
+ [:a0 4])))
+ )
;; =============================================================================
;; Seq matching
-(assert
- (= (let [x [1]]
- (match [x]
- [1] 1
- [([1] :seq)] 2
- :else []))
- 2))
-
-(assert
- (= (let [x [1 2 nil nil nil]]
- (match [x]
- [([1] :seq)] :a0
- [([1 2] :seq)] :a1
- [([1 2 nil nil nil] :seq)] :a2
- :else []))
- :a2))
-
-(assert
- (= (let [x '(1 2 4)
- y nil
- z nil]
- (match [x y z]
- [([1 2 b] :seq) _ _] [:a0 b]
- [([a 2 4] :seq) _ _] [:a1 a]
- :else []))
- [:a0 4]))
-
-(assert
- (= (let [x '(1 2 3)]
- (match [x]
- [([1 z 4] :seq)] z
- [([_ _ _] :seq)] :a2
- :else [])
- :a2)))
+(deftest test-seq-patterns
+ (testing "Seq patterns"
+ (is
+ (= (let [x [1]]
+ (match [x]
+ [1] 1
+ [([1] :seq)] 2
+ :else []))
+ 2))
+ (is
+ (= (let [x [1 2 nil nil nil]]
+ (match [x]
+ [([1] :seq)] :a0
+ [([1 2] :seq)] :a1
+ [([1 2 nil nil nil] :seq)] :a2
+ :else []))
+ :a2))
+ (is
+ (= (let [x '(1 2 4)
+ y nil
+ z nil]
+ (match [x y z]
+ [([1 2 b] :seq) _ _] [:a0 b]
+ [([a 2 4] :seq) _ _] [:a1 a]
+ :else []))
+ [:a0 4]))
+ (is
+ (= (let [x '(1 2 3)]
+ (match [x]
+ [([1 z 4] :seq)] z
+ [([_ _ _] :seq)] :a2
+ :else [])
+ :a2))))
+ )
;; =============================================================================
;; Map matching
-(assert
- (= (let [x {:a 1 :b 1}]
- (match [x]
- [{:a _ :b 2}] :a0
- [{:a 1 :b 1}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else nil))
- :a1))
-
-
-(assert
- (= (let [x {:a 1 :b 2}]
- (match [x]
- [{:a _ :b 2}] :a0
- [{:a 1 :b 1}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else nil))
- :a0))
-
-(assert
- (= (let [x {:c 3 :d 9 :e 4}]
- (match [x]
- [{:a _ :b 2}] :a0
- [{:a 1 :b 1}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else nil))
- :a2))
-
-(assert
- (= (let [x {:c 3 :e 4}]
- (match [x]
- [{:a _ :b 2}] :a0
- [{:a 1 :b 1}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else nil))
- nil))
-
-(assert
- (= (let [x {:a 1 :b 1}]
- (match [x]
- [{:a _ :b 1}] :a0
- [{:a 1 :b _}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else []))
- :a0))
-
-(assert
- (= (let [x {:a 1 :b 1 :c 1}]
- (match [x]
- [{:a _ :b 2}] :a0
- [{:a 1 :b _}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else []))
- :a1))
-
-(assert
- (= (let [x {:a 1 :b 1}]
- (match [x]
- [{:a _ :b 2}] :a0
- [{:a _ :b _}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else []))
- :a1))
-
-(assert
- (= (let [x {:a 1}]
- (match [x]
- [{:a 1 :b 1}] :a0
- [{:a _ :b _}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else []))
- []))
-
-(assert
- (= (let [x {:a 1 :b 1}]
- (match [x]
- [{:b 1}] :a0
- [{:a _ :b _}] :a1
- [{:a _ :b _}] :a2
- :else []))
- :a0))
-
-(assert
- (= (let [x {:a 1 :b 1}]
- (match [x]
- [{}] :a0
- [{:a _ :b _}] :a1
- [{:a 1 :b 1}] :a2
- :else []))
- :a0))
-
-(assert
- (= (let [x {:a 1 :b 1}]
- (match [x]
- [{:x nil :y nil}] :a0
- [{:a _ :b _}] :a1
- [{:a 1 :b 1}] :a2
- :else []))
- :a1))
-
-(assert
- (= (let [x {:a 1 :b 2}]
- (match [x]
- [({:a _ :b 2} :only [:a :b])] :a0
- [{:a 1 :c _}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else []))
- :a0))
-
-(assert
- (= (let [x {:a 1 :b 2 :c 3}]
- (match [x]
- [({:a _ :b 2} :only [:a :b])] :a0
- [{:a 1 :c _}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else []))
- :a1))
-
-(assert
- (= (let [x {:a 1 :b 2}]
- (match [x]
- [{:a a :b b}] [:a0 a b]
- :else []))
- [:a0 1 2]))
+(deftest test-map-patterns
+ (testing "Map patterns"
+ (is
+ (= (let [x {:a 1 :b 1}]
+ (match [x]
+ [{:a _ :b 2}] :a0
+ [{:a 1 :b 1}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else nil))
+ :a1))
+ (is
+ (= (let [x {:a 1 :b 2}]
+ (match [x]
+ [{:a _ :b 2}] :a0
+ [{:a 1 :b 1}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else nil))
+ :a0))
+ (is
+ (= (let [x {:c 3 :d 9 :e 4}]
+ (match [x]
+ [{:a _ :b 2}] :a0
+ [{:a 1 :b 1}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else nil))
+ :a2))
+ (is
+ (= (let [x {:c 3 :e 4}]
+ (match [x]
+ [{:a _ :b 2}] :a0
+ [{:a 1 :b 1}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else nil))
+ nil))
+ (is
+ (= (let [x {:a 1 :b 1}]
+ (match [x]
+ [{:a _ :b 1}] :a0
+ [{:a 1 :b _}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else []))
+ :a0))
+ (is
+ (= (let [x {:a 1 :b 1 :c 1}]
+ (match [x]
+ [{:a _ :b 2}] :a0
+ [{:a 1 :b _}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else []))
+ :a1))
+ (is
+ (= (let [x {:a 1 :b 1}]
+ (match [x]
+ [{:a _ :b 2}] :a0
+ [{:a _ :b _}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else []))
+ :a1))
+ (is
+ (= (let [x {:a 1}]
+ (match [x]
+ [{:a 1 :b 1}] :a0
+ [{:a _ :b _}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else []))
+ []))
+ (is
+ (= (let [x {:a 1 :b 1}]
+ (match [x]
+ [{:b 1}] :a0
+ [{:a _ :b _}] :a1
+ [{:a _ :b _}] :a2
+ :else []))
+ :a0))
+ (is
+ (= (let [x {:a 1 :b 1}]
+ (match [x]
+ [{}] :a0
+ [{:a _ :b _}] :a1
+ [{:a 1 :b 1}] :a2
+ :else []))
+ :a0))
+ (is
+ (= (let [x {:a 1 :b 1}]
+ (match [x]
+ [{:x nil :y nil}] :a0
+ [{:a _ :b _}] :a1
+ [{:a 1 :b 1}] :a2
+ :else []))
+ :a1))
+ (is
+ (= (let [x {:a 1 :b 2}]
+ (match [x]
+ [({:a _ :b 2} :only [:a :b])] :a0
+ [{:a 1 :c _}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else []))
+ :a0))
+ (is
+ (= (let [x {:a 1 :b 2 :c 3}]
+ (match [x]
+ [({:a _ :b 2} :only [:a :b])] :a0
+ [{:a 1 :c _}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else []))
+ :a1))
+ (is
+ (= (let [x {:a 1 :b 2}]
+ (match [x]
+ [{:a a :b b}] [:a0 a b]
+ :else []))
+ [:a0 1 2])))
+ )
;; =============================================================================
;; Seq pattern edge cases
-(assert
- (= (let [x '()]
- (match [x]
- [([] :seq)] :a0
- [([1 & r] :seq)] [:a1 r]
- :else []))
- :a0))
-
-(assert
- (= (let [x '(1 2)]
- (match [x]
- [([1] :seq)] :a0
- [([1 & r] :seq)] [:a1 r]
- :else []))
- [:a1 '(2)]))
-
-(assert
- (= (let [x '(1 2 3 4)]
- (match [x]
- [([1] :seq)] :a0
- [([_ 2 & ([a & b] :seq)] :seq)] [:a1 a b]
- :else []))
- [:a1 3 '(4)]))
+(deftest test-edge-cases-1
+ (testing "Edge cases 1"
+ (is
+ (= (let [x '()]
+ (match [x]
+ [([] :seq)] :a0
+ [([1 & r] :seq)] [:a1 r]
+ :else []))
+ :a0))
+ (is
+ (= (let [x '(1 2)]
+ (match [x]
+ [([1] :seq)] :a0
+ [([1 & r] :seq)] [:a1 r]
+ :else []))
+ [:a1 '(2)]))
+ (is
+ (= (let [x '(1 2 3 4)]
+ (match [x]
+ [([1] :seq)] :a0
+ [([_ 2 & ([a & b] :seq)] :seq)] [:a1 a b]
+ :else []))
+ [:a1 3 '(4)])))
+ )
;; =============================================================================
;; Or patterns
-(assert
- (= (let [x 4 y 6 z 9]
- (match [x y z]
- [(:or 1 2 3) _ _] :a0
- [4 (:or 5 6 7) _] :a1
- :else []))
- :a1))
-
-(assert
- (= (let [x '(1 2 3)
- y nil
- z nil]
- (match [x y z]
- [([1 (:or 3 4) 3] :seq) _ _] :a0
- [([1 (:or 2 3) 3] :seq) _ _] :a1
- :else []))
- :a1))
-
-(assert
- (= (let [x {:a 3}
- y nil
- z nil]
- (match [x y z]
- [{:a (:or 1 2)} _ _] :a0
- [{:a (:or 3 4)} _ _] :a1
- :else []))
- :a1))
+(deftest test-or-patterns
+ (testing "Or patterns"
+ (is
+ (= (let [x 4 y 6 z 9]
+ (match [x y z]
+ [(:or 1 2 3) _ _] :a0
+ [4 (:or 5 6 7) _] :a1
+ :else []))
+ :a1))
+ (is
+ (= (let [x '(1 2 3)
+ y nil
+ z nil]
+ (match [x y z]
+ [([1 (:or 3 4) 3] :seq) _ _] :a0
+ [([1 (:or 2 3) 3] :seq) _ _] :a1
+ :else []))
+ :a1))
+ (is
+ (= (let [x {:a 3}
+ y nil
+ z nil]
+ (match [x y z]
+ [{:a (:or 1 2)} _ _] :a0
+ [{:a (:or 3 4)} _ _] :a1
+ :else []))
+ :a1)))
+ )
;; =============================================================================
;; Guard patterns
@@ -278,442 +272,434 @@
;; =============================================================================
;; Edge cases
-(assert
- (= (let [a 1 b 1]
- (match [1 2]
- [a 3] :a1
- [1 2] :a2
- [2 _] :a5
- [_ 3] :a4
- :else :a3))
- :a2))
-
-(assert
- (= (let [x :as y :when z 1]
- (match [x y z]
- [a ':when 1] :success
- [:as _ 2] :fail
- :else :fail))
- :success))
-
-(assert
- (= (let [e '(+ 1 (+ 2 3))
- op (first e)
- op? #(= % op)]
- (match [e]
- [([p :guard op? x ([p2 :guard op? y z] :seq)] :seq)] (list p x y z)))
- '(+ 1 2 3)))
-
-(assert
- (= (let [e '(+ 1 (+ 2 3))]
- (match [e]
- [(['+ x (['+ y z] :seq)] :seq)] (list '+ x y z)))
- '(+ 1 2 3)))
-
-(assert
- (= (let [e 'quote
- f 10]
- (match [e f]
- ['quote quote] quote))
- 10))
-
-(assert
- (= (let [e '(:a (quote 10))]
- (match [e]
- [([quote (['quote 10] :seq)] :seq)] quote))
- :a))
+(deftest test-edge-cases-2
+ (testing "Edge cases 2"
+ (is
+ (= (let [a 1 b 1]
+ (match [1 2]
+ [a 3] :a1
+ [1 2] :a2
+ [2 _] :a5
+ [_ 3] :a4
+ :else :a3))
+ :a2))
+ (is
+ (= (let [x :as y :when z 1]
+ (match [x y z]
+ [a ':when 1] :success
+ [:as _ 2] :fail
+ :else :fail))
+ :success))
+ (is
+ (= (let [e '(+ 1 (+ 2 3))
+ op (first e)
+ op? #(= % op)]
+ (match [e]
+ [([p :guard op? x ([p2 :guard op? y z] :seq)] :seq)] (list p x y z)))
+ '(+ 1 2 3)))
+ (is
+ (= (let [e '(+ 1 (+ 2 3))]
+ (match [e]
+ [(['+ x (['+ y z] :seq)] :seq)] (list '+ x y z)))
+ '(+ 1 2 3)))
+ (is
+ (= (let [e 'quote
+ f 10]
+ (match [e f]
+ ['quote quote] quote))
+ 10))
+ (is
+ (= (let [e '(:a (quote 10))]
+ (match [e]
+ [([quote (['quote 10] :seq)] :seq)] quote))
+ :a))))
;; =============================================================================
;; As pattern
-(assert
- (= (let [v [[1 2]]]
- (match [v]
- [([3 1] :seq)] :a0
- [([(([1 a] :seq) :as b)] :seq)] [:a1 a b]
- :else []))
- [:a1 2 [1 2]]))
+(deftest test-as-pattern
+ (is
+ (= (let [v [[1 2]]]
+ (match [v]
+ [([3 1] :seq)] :a0
+ [([(([1 a] :seq) :as b)] :seq)] [:a1 a b]
+ :else []))
+ [:a1 2 [1 2]])))
;; =============================================================================
;; Else cases
-(assert
- (= (let [v [1]]
- (match [v]
- [2] 1
- :else 21))
- 21))
-
-(assert
- (= (let [v [[1 2]]]
- (match [v]
- [([1 3] :seq)] 1
- :else 21))
- 21))
-
-(assert
- (= (let [v {:a 1}]
- (match [v]
- [{:a a}] 1
- :else 21))
- 1))
-
-(assert
- (= (let [v 3]
- (match [v]
- [(:or 1 2)] :a0
- :else :a1))
- :a1))
-
-(assert
- (= (->> (range 1 16)
- (map (fn [x]
- (match [(mod x 3) (mod x 5)]
- [0 0] "FizzBuzz"
- [0 _] "Fizz"
- [_ 0] "Buzz"
- :else (str x)))))
- '("1" "2" "Fizz" "4" "Buzz" "Fizz" "7" "8" "Fizz" "Buzz" "11" "Fizz" "13" "14" "FizzBuzz")))
+(deftest test-else
+ (testing "Test :else support"
+ (is
+ (= (let [v [1]]
+ (match [v]
+ [2] 1
+ :else 21))
+ 21))
+ (is
+ (= (let [v [[1 2]]]
+ (match [v]
+ [([1 3] :seq)] 1
+ :else 21))
+ 21))
+ (is
+ (= (let [v {:a 1}]
+ (match [v]
+ [{:a a}] 1
+ :else 21))
+ 1))
+ (is
+ (= (let [v 3]
+ (match [v]
+ [(:or 1 2)] :a0
+ :else :a1))
+ :a1))
+ (is
+ (= (->> (range 1 16)
+ (map (fn [x]
+ (match [(mod x 3) (mod x 5)]
+ [0 0] "FizzBuzz"
+ [0 _] "Fizz"
+ [_ 0] "Buzz"
+ :else (str x)))))
+ '("1" "2" "Fizz" "4" "Buzz" "Fizz" "7" "8" "Fizz" "Buzz" "11" "Fizz" "13" "14" "FizzBuzz"))))
+ )
;; =============================================================================
;; Single expressions
-(assert
- (= (let [x 3]
- (match x
- 1 :a0
- 2 :a1
- :else :a2))
- :a2))
-
-(assert
- (= (let [x 3]
- (match (mod x 2)
- 1 :a0
- 2 :a1
- :else :a2))
- :a0))
+(deftest test-single-match-sguar
+ (testing "Single match sugar"
+ (is
+ (= (let [x 3]
+ (match x
+ 1 :a0
+ 2 :a1
+ :else :a2))
+ :a2))
+ (is
+ (= (let [x 3]
+ (match (mod x 2)
+ 1 :a0
+ 2 :a1
+ :else :a2))
+ :a0))))
;; =============================================================================
;; Locals matching
-(assert
- (= (let [x 2
- y 2]
- (match [x]
- [0] :a0
- [1] :a1
- [y] :a2
- :else :a3))
- :a2))
-
-(assert
- (= (let [x 2]
- (match [x]
- [0] :a0
- [1] :a1
- [2] :a2
- :else :a3))
- :a2))
-
-(assert
- (= (let [a 1]
- (match [1 2]
- [1 3] :a0
- [a 2] :a1
- :else :a2))
- :a1))
+(deftest test-locals-matching
+ (testing "Locals matching"
+ (is
+ (= (let [x 2
+ y 2]
+ (match [x]
+ [0] :a0
+ [1] :a1
+ [y] :a2
+ :else :a3))
+ :a2))
+ (is
+ (= (let [x 2]
+ (match [x]
+ [0] :a0
+ [1] :a1
+ [2] :a2
+ :else :a3))
+ :a2))
+ (is
+ (= (let [a 1]
+ (match [1 2]
+ [1 3] :a0
+ [a 2] :a1
+ :else :a2))
+ :a1))))
;; =============================================================================
;; More edgecases
-(assert
- (= (match [true false]
- [true false] 1
- [false true] 2
- :else (throw (js/Error. "Shouldn't be here")))
- 1))
-
-(assert
- (= (let [x [1 2]]
- (match [x]
- [(:or [1 2] [3 4] [5 6] [7 8] [9 10])] :a0
- :else (throw (js/Error. "Shouldn't be here"))))
- :a0))
-
-(assert
- (= (let [_ 1
- x 2
- y 3]
- (match [x y]
- [1 1] :a0
- [_ 2] :a1
- [2 3] :a2
- :else :a3))
- :a2))
+(deftest test-edge-cases-3
+ (testing "Edge cases 3"
+ (is
+ (= (match [true false]
+ [true false] 1
+ [false true] 2
+ :else (throw (js/Error. "Shouldn't be here")))
+ 1))
+
+ (is
+ (= (let [x [1 2]]
+ (match [x]
+ [(:or [1 2] [3 4] [5 6] [7 8] [9 10])] :a0
+ :else (throw (js/Error. "Shouldn't be here"))))
+ :a0))
+
+ (is
+ (= (let [_ 1
+ x 2
+ y 3]
+ (match [x y]
+ [1 1] :a0
+ [_ 2] :a1
+ [2 3] :a2
+ :else :a3))
+ :a2)))
+ )
;; =============================================================================
;; Vector patterns
-(assert
- (= (let [x [1 2 3]]
- (match [x]
- [([_ _ 2] ::clojure.core.match/vector)] :a0
- [([1 1 3] ::clojure.core.match/vector)] :a1
- [([1 2 3] ::clojure.core.match/vector)] :a2
- :else :a3))
- :a2))
-
-(assert
- (= (let [n [:black [:red [:red 1 2 3] 3 4] 5 6]]
- (match [n]
- [(:or [:black [:red [:red a x b] y c] z d]
- [:black [:red a x [:red b y c]] z d]
- [:black a x [:red [:red b y c] z d]]
- [:black a x [:red b y [:red c z d]]])] :balance
- :else :valid))
- :balance))
-
-(assert
- (= (let [v [1 2 3 4]]
- (match [v]
- [([1 1 3 & r] ::clojure.core.match/vector)] :a0
- [([1 2 4 & r] ::clojure.core.match/vector)] :a1
- [([1 2 3 & r] ::clojure.core.match/vector)] :a2
- :else :a3))
- :a2))
-
-(assert
- (= (let [v [1 2 3 4]]
- (let [v [1 2 3 4]]
+(deftest test-vector-patterns
+ (is
+ (= (let [x [1 2 3]]
+ (match [x]
+ [([_ _ 2] ::clojure.core.match/vector)] :a0
+ [([1 1 3] ::clojure.core.match/vector)] :a1
+ [([1 2 3] ::clojure.core.match/vector)] :a2
+ :else :a3))
+ :a2))
+ (is
+ (= (let [n [:black [:red [:red 1 2 3] 3 4] 5 6]]
+ (match [n]
+ [(:or [:black [:red [:red a x b] y c] z d]
+ [:black [:red a x [:red b y c]] z d]
+ [:black a x [:red [:red b y c] z d]]
+ [:black a x [:red b y [:red c z d]]])] :balance
+ :else :valid))
+ :balance))
+ (is
+ (= (let [v [1 2 3 4]]
(match [v]
[([1 1 3 & r] ::clojure.core.match/vector)] :a0
- [([1 2 & r] ::clojure.core.match/vector)] :a1
- :else :a3)))
- :a1))
-
-(assert
- (= (let [node 1]
- (match [node]
- [[1]] :a0
- [a] a
- :else :a1))
- 1))
-
-(assert
- (= (let [v []]
- (match [v]
- [[]] 1
- :else 2))
- 1))
-
-(assert
- (= (let [v [1 2]]
- (match [v]
- [[]] :a0
- [[x & r]] :a1
- :else :a2))
- :a1))
-
-(assert
- (= (let [v [[1 2]]]
- (match [v]
- [[3 1]] :a0
- [[([1 a] :as b)]] [:a1 a b]
- :else :a2))
- [:a1 2 [1 2]]))
+ [([1 2 4 & r] ::clojure.core.match/vector)] :a1
+ [([1 2 3 & r] ::clojure.core.match/vector)] :a2
+ :else :a3))
+ :a2))
+ (is
+ (= (let [v [1 2 3 4]]
+ (let [v [1 2 3 4]]
+ (match [v]
+ [([1 1 3 & r] ::clojure.core.match/vector)] :a0
+ [([1 2 & r] ::clojure.core.match/vector)] :a1
+ :else :a3)))
+ :a1))
+ (is
+ (= (let [node 1]
+ (match [node]
+ [[1]] :a0
+ [a] a
+ :else :a1))
+ 1))
+ (is
+ (= (let [v []]
+ (match [v]
+ [[]] 1
+ :else 2))
+ 1))
+ (is
+ (= (let [v [1 2]]
+ (match [v]
+ [[]] :a0
+ [[x & r]] :a1
+ :else :a2))
+ :a1))
+ (is
+ (= (let [v [[1 2]]]
+ (match [v]
+ [[3 1]] :a0
+ [[([1 a] :as b)]] [:a1 a b]
+ :else :a2))
+ [:a1 2 [1 2]]))
+ )
;; =============================================================================
;; Yet more edge cases
-(assert
- (= (let [l '(1 2 3)]
- (match [l]
- [([a & [b & [c]]] :seq)] :a0
- :else :a1))
- :a0))
-
-(assert
- (= (match [[:pow :x 2]]
- [[:pow arg pow]] 0
- [[:mult & args]] 1
- :else 2)
- 0))
-
-(assert
- (= (match [false]
- [false] true)
- true))
-
-(assert
- (= (match [[:plus 1 2 3]]
- [[:pow arg pow]] 0
- [[:plus & args]] 1
- :else 2))
- 1)
-
-(assert
- (= (let [x {:a 1 :b 2 :c 10 :d 30}]
- (match [x]
- [({:a _ :b _ :c _ :d _} :only [:a :b :c :d])] :a-1
- [({:a _ :b 2} :only [:a :b])] :a0
- [{:a 1 :c _}] :a1
- [{:c 3 :d _ :e 4}] :a2
- :else []))
- :a-1))
-
-(assert
- (and (= (let [m {:a 1}]
- (match [m]
- [({:a 1} :only [:a])] :a0
- :else :a1))
- :a0)
- (= (let [m {:a 1 :b 2}]
- (match [m]
- [({:a 1} :only [:a])] :a0
- :else :a1))
- :a1)))
-
-(assert
- (= (let [m {:foo 1 "bar" 2}]
- (match [m]
- [{:foo 1 "bar" 2}] :a0
- :else :a1))
- :a0))
+(deftest test-edge-cases-4
+ (testing "Edge cases 4"
+ (is
+ (= (let [l '(1 2 3)]
+ (match [l]
+ [([a & [b & [c]]] :seq)] :a0
+ :else :a1))
+ :a0))
+
+ (is
+ (= (match [[:pow :x 2]]
+ [[:pow arg pow]] 0
+ [[:mult & args]] 1
+ :else 2)
+ 0))
+
+ (is
+ (= (match [false]
+ [false] true)
+ true))
+
+ (is
+ (= (match [[:plus 1 2 3]]
+ [[:pow arg pow]] 0
+ [[:plus & args]] 1
+ :else 2))
+ 1)
+
+ (is
+ (= (let [x {:a 1 :b 2 :c 10 :d 30}]
+ (match [x]
+ [({:a _ :b _ :c _ :d _} :only [:a :b :c :d])] :a-1
+ [({:a _ :b 2} :only [:a :b])] :a0
+ [{:a 1 :c _}] :a1
+ [{:c 3 :d _ :e 4}] :a2
+ :else []))
+ :a-1))
+
+ (is
+ (and (= (let [m {:a 1}]
+ (match [m]
+ [({:a 1} :only [:a])] :a0
+ :else :a1))
+ :a0)
+ (= (let [m {:a 1 :b 2}]
+ (match [m]
+ [({:a 1} :only [:a])] :a0
+ :else :a1))
+ :a1)))
+
+ (is
+ (= (let [m {:foo 1 "bar" 2}]
+ (match [m]
+ [{:foo 1 "bar" 2}] :a0
+ :else :a1))
+ :a0)))
+ )
;; =============================================================================
;; Errors
-(assert
- (= (try
- (match :a :a (throw (js/Error.)) :else :c)
- (catch js/Error e
- :d))
- :d))
+(deftest test-errors
+ (is
+ (= (try
+ (match :a :a (throw (js/Error.)) :else :c)
+ (catch js/Error e
+ :d))
+ :d)))
;; =============================================================================
;; Match order
-(assert
- (= (let [x '(1 2) y 1]
- (match [x y]
- [([1] :seq) _] :a0
- [_ 1] :a1
- [([1 2] :seq) _] :a2
- [_ 2] :a3
- :else :a4))
- :a1))
-
-(assert
- (= (let [x '(1 2) y 1]
- (match [x y]
- [([1] :seq) _] :a0
- [([1 2] :seq) _] :a2
- [_ 1] :a1
- [_ 2] :a3
- :else :a4))
- :a2))
-
-(assert
- (= (let [x '(1 2) y 3]
- (match [x y]
- [([1] :seq) _] :a0
- [_ 1] :a1
- [([1 2] :seq) _] :a2
- [_ 2] :a3
- :else :a4))
- :a2))
-
-(assert
- (= (let [x '(1) y 3]
- (match [x y]
- [([1] :seq) _] :a0
- [_ 1] :a1
- [([1 2] :seq) _] :a2
- [_ 2] :a3
- :else :a4))
- :a0))
-
-(assert
- (= (let [x '(1 2 3) y 2]
- (match [x y]
- [([1] :seq) _] :a0
- [_ 1] :a1
- [([1 2] :seq) _] :a2
- [_ 2] :a3
- :else :a4))
- :a3))
-
-(assert
- (= (match [["foo"]]
- [["foo"]] :a0
- [["foo" a]] :a1
- [["baz"]] :a2
- [["baz" a b]] :a3
- :else :a4)
- :a0))
-
-(assert
- (= (match [[2]]
- [[1]] :a0
- [1] :a1
- [[2]] :a2
- [2] :a3
- :else :a4)
- :a2))
-
-(assert
- (= ((fn [x done]
- (if done
- done
- (match [x]
- [[1]] (recur x :a0)
- [1] (recur x :a1)
- [[2]] (recur x :a2)
- [2] (recur x :a3)
- :else :a4))) [2] false)
- :a2))
-
-(assert
- (= ((fn [x done]
- (if done
- done
- (match [x]
- [[1]] (recur x :a0)
- [1] (recur x :a1)
- [[2]] (recur x :a2)
- [2] (recur x :a3)
- [3] (recur x :a4)
- [[3]] (recur x :a4)
- :else :a5))) [3] false)
- :a4))
-
-(assert
- (= (match [[2]]
- [1] :a0
- [[1]] :a1
- [2] :a2
- [[2]] :a3
- :else :a4)
- :a3))
-
-(assert
- (= (let [xs [:c]]
- (match xs
- [:a] :a0
- [:b b] :a1
- [:c] :a2
- :else :a3))
- :a2))
-
-(assert
- (= (let [xs [1 2 3]]
- (match [xs]
- [([1 2 4] :seq)] :a0
- [[1 2 5]] :a1
- [([1 2 6] :seq)] :a2
- [[1 2 3]] :a3))
- :a3))
+(deftest match-order
+ (testing "Match order"
+ (is
+ (= (let [x '(1 2) y 1]
+ (match [x y]
+ [([1] :seq) _] :a0
+ [_ 1] :a1
+ [([1 2] :seq) _] :a2
+ [_ 2] :a3
+ :else :a4))
+ :a1))
+ (is
+ (= (let [x '(1 2) y 1]
+ (match [x y]
+ [([1] :seq) _] :a0
+ [([1 2] :seq) _] :a2
+ [_ 1] :a1
+ [_ 2] :a3
+ :else :a4))
+ :a2))
+ (is
+ (= (let [x '(1 2) y 3]
+ (match [x y]
+ [([1] :seq) _] :a0
+ [_ 1] :a1
+ [([1 2] :seq) _] :a2
+ [_ 2] :a3
+ :else :a4))
+ :a2))
+ (is
+ (= (let [x '(1) y 3]
+ (match [x y]
+ [([1] :seq) _] :a0
+ [_ 1] :a1
+ [([1 2] :seq) _] :a2
+ [_ 2] :a3
+ :else :a4))
+ :a0))
+ (is
+ (= (let [x '(1 2 3) y 2]
+ (match [x y]
+ [([1] :seq) _] :a0
+ [_ 1] :a1
+ [([1 2] :seq) _] :a2
+ [_ 2] :a3
+ :else :a4))
+ :a3))
+ (is
+ (= (match [["foo"]]
+ [["foo"]] :a0
+ [["foo" a]] :a1
+ [["baz"]] :a2
+ [["baz" a b]] :a3
+ :else :a4)
+ :a0))
+ (is
+ (= (match [[2]]
+ [[1]] :a0
+ [1] :a1
+ [[2]] :a2
+ [2] :a3
+ :else :a4)
+ :a2))
+ (is
+ (= ((fn [x done]
+ (if done
+ done
+ (match [x]
+ [[1]] (recur x :a0)
+ [1] (recur x :a1)
+ [[2]] (recur x :a2)
+ [2] (recur x :a3)
+ :else :a4))) [2] false)
+ :a2))
+ (is
+ (= ((fn [x done]
+ (if done
+ done
+ (match [x]
+ [[1]] (recur x :a0)
+ [1] (recur x :a1)
+ [[2]] (recur x :a2)
+ [2] (recur x :a3)
+ [3] (recur x :a4)
+ [[3]] (recur x :a4)
+ :else :a5))) [3] false)
+ :a4))
+ (is
+ (= (match [[2]]
+ [1] :a0
+ [[1]] :a1
+ [2] :a2
+ [[2]] :a3
+ :else :a4)
+ :a3))
+ (is
+ (= (let [xs [:c]]
+ (match xs
+ [:a] :a0
+ [:b b] :a1
+ [:c] :a2
+ :else :a3))
+ :a2))
+ (is
+ (= (let [xs [1 2 3]]
+ (match [xs]
+ [([1 2 4] :seq)] :a0
+ [[1 2 5]] :a1
+ [([1 2 6] :seq)] :a2
+ [[1 2 3]] :a3))
+ :a3)))
+ )
;; =============================================================================
;; Extending objects to pattern matching
@@ -729,33 +715,35 @@
:year (.getFullYear this)
not-found)))
-(assert
- (= (match [(js/Date. 2010 10 1 12 30)]
- [{:year 2009 :month a}] a
- [{:year (:or 2010 2011) :month b}] b
- :else :wrong)
- 10))
+(deftest test-match-on-extended-type
+ (is
+ (= (match [(js/Date. 2010 10 1 12 30)]
+ [{:year 2009 :month a}] a
+ [{:year (:or 2010 2011) :month b}] b
+ :else :wrong)
+ 10)))
;; =============================================================================
;; Arrays
-(assert
- (= (let [x (int-array [1 2 3])]
- (match [^ints x]
- [[_ _ 2]] :a0
- [[1 1 3]] :a1
- [[1 2 3]] :a2
- :else :a3))
- :a2))
-
-(assert
- (= (let [x (object-array [:foo :bar :baz])]
- (match [^objects x]
- [[_ _ :bar]] :a0
- [[:foo :foo :bar]] :a1
- [[:foo :bar :baz]] :a2
- :else :a3))
- :a2))
+(deftest test-array-match1
+ (is
+ (= (let [x (int-array [1 2 3])]
+ (match [^ints x]
+ [[_ _ 2]] :a0
+ [[1 1 3]] :a1
+ [[1 2 3]] :a2
+ :else :a3))
+ :a2))
+ (is
+ (= (let [x (object-array [:foo :bar :baz])]
+ (match [^objects x]
+ [[_ _ :bar]] :a0
+ [[:foo :foo :bar]] :a1
+ [[:foo :bar :baz]] :a2
+ :else :a3))
+ :a2))
+ )
(defn B [l v r]
(let [o (make-array 4)]
@@ -773,10 +761,11 @@
[:black a x [:red b y [:red c z d]]])] :balance
:else :balanced))
-(assert
- (= (let [node (B nil nil (R nil nil (R nil nil nil)))]
- (balance-array node))
- :balance))
+(deftest test-red-black-match
+ (is
+ (= (let [node (B nil nil (R nil nil (R nil nil nil)))]
+ (balance-array node))
+ :balance)))
(println "benchmarking")
@@ -860,74 +849,122 @@
(let [node (B nil nil (R nil nil (R nil nil nil)))]
(time
- (dotimes [_ 1e7]
+ (dotimes [_ 1e6]
(balance-array node))))
+;; fn application
+
+(deftest test-app-patterns
+ (testing "Function application patterns"
+ (let [n 1]
+ (is
+ (= (match [n]
+ [(1 :<< inc)] :one
+ [(2 :<< inc)] :two
+ :else :oops)
+ :two))
+ (is
+ (= (match [n]
+ [(1 :<< inc)] :one
+ [(3 :<< #(* % 3))] :three
+ :else :oops)
+ :three)))
+ (let [v [1 2 4 3]]
+ (is
+ (= (match [v]
+ [(([1 2 4 5] :seq) :<< sort)] :this-sort
+ [(([3 _ _ _] :seq) :<< reverse)] :last-is-three
+ :else :oops)
+ :last-is-three))
+ (is
+ (= (match [v]
+ [((:or 3 4) :<< count)] :three-or-four
+ [(5 :<< count)] :five
+ :else :oops)
+ :three-or-four)))
+ (let [v [1 2 3]
+ m {:a 2 :b 2}]
+ (is
+ (= (match [v]
+ [[1 (3 :<< inc) 3]] :match1
+ [[1 (4 :<< inc) 3]] :match2
+ :else :no-match)
+ :match1))
+ (is
+ (= (match [m]
+ [{:a (2 :<< inc) :b _}] :match1
+ [{:a (3 :<< inc) :b _}] :match2
+ :else :no-match)
+ :match2)))))
+
;; =============================================================================
;; Tickets
-(assert (= (match 3 x x) 3))
-(assert (= (match 'my-sym a a) 'my-sym))
-
-(assert
- (= (let [xqq {:cz 1 :dz 2}]
- (match [xqq]
- [{:z a :zz b}] [:a0 a b]
- [{:cz a :dz b}] [:a2 a b]
- :else []))
- [:a2 1 2]))
-
-(assert
- (= (let [xmm {:bz 2}]
- (match [xmm]
- [{:az a}] [:a0 a]
- [{:bz b}] [:a1 b]
- :else []))
- [:a1 2]))
-
-(assert
- (= (match (vector)
- ([(re :guard string?)] :seq) 4
- [] 6)
- 6))
-
-(assert
- (= (match [ [1 2] ] [([& _] :seq)] true)
- true))
-
-(assert
- (= (let [x []]
- (match [x]
- [[h & t]] [h t]
- :else :nomatch))
- :nomatch))
-
-(assert
- (= (let [x [1]]
- (match [x]
- [[h & t]] [h t]
- :else :nomatch))
- [1 []]))
-
-(assert
- (= (match [[:x]]
- [[m n & _]] 1
- :else nil)
- nil))
-
-(assert
- (= (let [l '(1 2 3)]
- (match [l]
- [([a & [b & [c d]]] :seq)] :a0
- :else :a1))
- :a1))
-
-(assert
- (= (let [x ()]
- (match [x]
- [([h & t] :seq)] [h t]
- [_] :a1))
- :a1))
+(deftest test-misc-tickets
+ (testing "Misc. tickets"
+ (is (= (match 3 x x) 3))
+ (is (= (match 'my-sym a a) 'my-sym))
+
+ (is
+ (= (let [xqq {:cz 1 :dz 2}]
+ (match [xqq]
+ [{:z a :zz b}] [:a0 a b]
+ [{:cz a :dz b}] [:a2 a b]
+ :else []))
+ [:a2 1 2]))
+
+ (is
+ (= (let [xmm {:bz 2}]
+ (match [xmm]
+ [{:az a}] [:a0 a]
+ [{:bz b}] [:a1 b]
+ :else []))
+ [:a1 2]))
+
+ (is
+ (= (match (vector)
+ ([(re :guard string?)] :seq) 4
+ [] 6)
+ 6))
+
+ (is
+ (= (match [ [1 2] ] [([& _] :seq)] true)
+ true))
+
+ (is
+ (= (let [x []]
+ (match [x]
+ [[h & t]] [h t]
+ :else :nomatch))
+ :nomatch))
+
+ (is
+ (= (let [x [1]]
+ (match [x]
+ [[h & t]] [h t]
+ :else :nomatch))
+ [1 []]))
+
+ (is
+ (= (match [[:x]]
+ [[m n & _]] 1
+ :else nil)
+ nil))
+
+ (is
+ (= (let [l '(1 2 3)]
+ (match [l]
+ [([a & [b & [c d]]] :seq)] :a0
+ :else :a1))
+ :a1))
+
+ (is
+ (= (let [x ()]
+ (match [x]
+ [([h & t] :seq)] [h t]
+ [_] :a1))
+ :a1)))
+ )
(defn get-meaning
[paragraph line blank mode theme annotation]
@@ -944,42 +981,39 @@
[_ _ false nil false false ] "monaco-supplemental"
:else "monaco-generic"))
-(assert
- (= (get-meaning 2 nil false nil false false)
- "monaco-following-action"))
-
-;; MATCH-82
-
-(assert
- (= (let [x 1]
- (match 2
- x 1
- _ 2))
- 2))
-
-;; MATCH-83
-
-(assert
- (= (let [x [1 2]]
- (match x
- [0 _ _ _] :a
- [1 & _] :b
- _ :c))
- :b))
-
-;; MATCH-84
-
-(assert
- (= (let [v [3 2 3 4]]
- (match [v]
- [[1 1 3]] :a0
- [[3 & r]] :a2))
- :a2))
-
-;; Test macros
+(deftest test-large-match
+ (is
+ (= (get-meaning 2 nil false nil false false)
+ "monaco-following-action")))
+
+(deftest test-match-82
+ (is
+ (= (let [x 1]
+ (match 2
+ x 1
+ _ 2))
+ 2)))
+
+(deftest test-match-83
+ (is
+ (= (let [x [1 2]]
+ (match x
+ [0 _ _ _] :a
+ [1 & _] :b
+ _ :c))
+ :b)))
+
+(deftest test-match-84
+ (is
+ (= (let [v [3 2 3 4]]
+ (match [v]
+ [[1 1 3]] :a0
+ [[3 & r]] :a2))
+ :a2)))
-(assert
- (= ((mt/test1))
- :match))
+(deftest test-macros
+ (is
+ (= ((mt/test1))
+ :match)))
-(println "Tests completed without exception.")
+(run-tests)
=====================================
src/test/clojure/clojure/core/match/test/core.clj
=====================================
@@ -370,7 +370,7 @@
(deftest map-pattern-interop-1
(is (= (let [d (java.util.Date. 2010 10 1 12 30)]
- (match [d]
+ (matchm [d]
[{:year 2009 :month a}] [:a0 a]
[{:year (:or 2010 2011) :month b}] [:a1 b]
:else []))
@@ -526,9 +526,9 @@
(deftest vector-pattern-match-1
(is (= (let [x [1 2 3]]
(match [x]
- [([_ _ 2] ::clojure.core.match/vector)] :a0
- [([1 1 3] ::clojure.core.match/vector)] :a1
- [([1 2 3] ::clojure.core.match/vector)] :a2
+ [([_ _ 2] :clojure.core.match/vector)] :a0
+ [([1 1 3] :clojure.core.match/vector)] :a1
+ [([1 2 3] :clojure.core.match/vector)] :a2
:else :a3))
:a2)))
@@ -595,9 +595,9 @@
(deftest vector-pattern-rest-1
(is (= (let [v [1 2 3 4]]
(match [v]
- [([1 1 3 & r] ::clojure.core.match/vector)] :a0
- [([1 2 4 & r] ::clojure.core.match/vector)] :a1
- [([1 2 3 & r] ::clojure.core.match/vector)] :a2
+ [([1 1 3 & r] :clojure.core.match/vector)] :a0
+ [([1 2 4 & r] :clojure.core.match/vector)] :a1
+ [([1 2 3 & r] :clojure.core.match/vector)] :a2
:else :a3))
:a2)))
@@ -605,8 +605,8 @@
(is (= (let [v [1 2 3 4]]
(let [v [1 2 3 4]]
(match [v]
- [([1 1 3 & r] ::clojure.core.match/vector)] :a0
- [([1 2 & r] ::clojure.core.match/vector)] :a1
+ [([1 1 3 & r] :clojure.core.match/vector)] :a0
+ [([1 2 & r] :clojure.core.match/vector)] :a1
:else :a3)))
:a1)))
@@ -820,6 +820,46 @@
[[1 2 3]] :a3))
:a3)))
+(deftest match-app-1
+ (let [n 1]
+ (is (= (match [n]
+ [(1 :<< inc)] :one
+ [(2 :<< inc)] :two
+ :else :oops)
+ :two))
+ (is (= (match [n]
+ [(1 :<< inc)] :one
+ [(3 :<< #(* % 3))] :three
+ :else :oops)
+ :three))))
+
+(deftest match-app-2
+ (let [v [1 2 4 3]]
+ (is (= (match [v]
+ [(([1 2 4 5] :seq) :<< sort)] :this-sort
+ [(([3 _ _ _] :seq) :<< reverse)] :last-is-three
+ :else :oops)
+ :last-is-three))
+ (is (= (match [v]
+ [((:or 3 4) :<< count)] :three-or-four
+ [(5 :<< count)] :five
+ :else :oops)
+ :three-or-four))))
+
+(deftest match-app-3
+ (let [v [1 2 3]
+ m {:a 2 :b 2}]
+ (is (= (match [v]
+ [[1 (3 :<< inc) 3]] :match1
+ [[1 (4 :<< inc) 3]] :match2
+ :else :no-match)
+ :match1))
+ (is (= (match [m]
+ [{:a (2 :<< inc) :b _}] :match1
+ [{:a (3 :<< inc) :b _}] :match2
+ :else :no-match)
+ :match2))))
+
;; =============================================================================
;; Tickets
=====================================
src/test/clojure/clojure/core/match/test/date.clj
=====================================
@@ -1,11 +1,11 @@
(ns clojure.core.match.test.date
(:use clojure.test)
- (:use [clojure.core.match :only [match]])
+ (:use [clojure.core.match :only [matchm]])
(:use clojure.core.match.date))
(deftest date-test1
- (is (= (match [(java.util.Date. 2010 10 1 12 30)]
- [{:year 2009 :month a}] a
- [{:year (:or 2010 2011) :month b}] b
- :else :wrong)
- 10)))
+ (is (= (matchm [(java.util.Date. 2010 10 1 12 30)]
+ [{:year 2009 :month a}] a
+ [{:year (:or 2010 2011) :month b}] b
+ :else :wrong)
+ 10)))
=====================================
src/test/clojure/clojure/core/match/test/java.clj
=====================================
@@ -7,16 +7,16 @@
(bean-match java.util.Date)
(deftest bean-match-date
- (is (= 10 (match [(java.util.Date. 2009 10 1 12 30)]
- [{:year 2009 :month a}] a
- [{:year (:or 2010 2011) :month b}] b
- :else :wrong))))
+ (is (= 10 (matchm [(java.util.Date. 2009 10 1 12 30)]
+ [{:year 2009 :month a}] a
+ [{:year (:or 2010 2011) :month b}] b
+ :else :wrong))))
(bean-match java.io.File)
(deftest bean-match-file
(is (= (.getAbsolutePath (java.io.File. "."))
- (match [(java.io.File. ".")]
- [{:directory? true :absolute-path p}] p
- :else :wrong))))
+ (matchm [(java.io.File. ".")]
+ [{:directory? true :absolute-path p}] p
+ :else :wrong))))
View it on GitLab: https://salsa.debian.org/clojure-team/core-match-clojure/-/commit/a751e8de486bb22c2dd4a42654bef41a3c63089e
--
View it on GitLab: https://salsa.debian.org/clojure-team/core-match-clojure/-/commit/a751e8de486bb22c2dd4a42654bef41a3c63089e
You're receiving this email because of your account on salsa.debian.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20201130/a1dc5304/attachment.html>
More information about the pkg-java-commits
mailing list