Lambder
Lambder

Reputation: 2993

how can I bind the matched :or to a variable in core.match

how can I bind the matched :or or to a variable in 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=>

Upvotes: 2

Views: 144

Answers (1)

OlegTheCat
OlegTheCat

Reputation: 4513

Seems that core.match doesn't allow :or patterns to be bound to symbol. But you always can emulate :or behavior with guards and guarded patterns can be bound to symbol:

(def v [1 2])

(match v
  [(x :guard #{1 2}) 2] [:foo x]
  :else :bar) ;; => [:foo 1]

Upvotes: 4

Related Questions