如何将匹配的 :or 绑定到 core.match 中的变量
how can I bind the matched :or to a variable in core.match
如何绑定匹配的 :or
或 core.match 中的变量?
user=> (use 'clojure.core.match)
user=> (let [v 2]
#_=> (match [v]
#_=> [((:or 1 2) :as x)] [:foo x]
#_=> :else :bar))
java.lang.RuntimeException: Unable to resolve symbol: x in this context
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: x in this context, compiling:(/private/var/folders/jp/y5hpfwq962x476pm_1knzr8c0000gn/T/form-init7072120633012300984.clj:2:10)
user=>
似乎 core.match
不允许 :or
模式绑定到符号。但是你总是可以用守卫模拟 :or
行为,守卫模式可以绑定到符号:
(def v [1 2])
(match v
[(x :guard #{1 2}) 2] [:foo x]
:else :bar) ;; => [:foo 1]
如何绑定匹配的 :or
或 core.match 中的变量?
user=> (use 'clojure.core.match)
user=> (let [v 2]
#_=> (match [v]
#_=> [((:or 1 2) :as x)] [:foo x]
#_=> :else :bar))
java.lang.RuntimeException: Unable to resolve symbol: x in this context
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: x in this context, compiling:(/private/var/folders/jp/y5hpfwq962x476pm_1knzr8c0000gn/T/form-init7072120633012300984.clj:2:10)
user=>
似乎 core.match
不允许 :or
模式绑定到符号。但是你总是可以用守卫模拟 :or
行为,守卫模式可以绑定到符号:
(def v [1 2])
(match v
[(x :guard #{1 2}) 2] [:foo x]
:else :bar) ;; => [:foo 1]