Hi,

I've recently started using the matchure library for pattern matching
(https://github.com/dcolthorp/matchure).

Basically, I'd like match a number of different values and bind the
first match to a symbol.  In matchure, binding to a variable can be
done like this:

(if-match [(and ?c "Hi") "Hi"] c) ; Will return "Hi"

I have a map:

(def *chars* { "Grace" :Grace
               "grace" :Grace
               "Trip" :Trip
               "trip" :Trip})

And I'd like to produce something like this:

(if-match [(or
                  (and ?c #"Grace")
                  (and ?c #"grace")
                  (and ?c #"Trip")
                  (and ?c #"trip"))
                  "Trip"]
                  c)

>From something like this:

(if-match [(match-character ?c) "Trip"] c)

Currently, I have the following macro:

(defmacro match-character
  "Creates a pattern-matching OR that binds the provided symbol
to the first matched name."
  [x]
  `(or
    ~@(map
      (fn [[k _]]
        `(and ~x ~(re-pattern k)))
      *chars*)))

It produces the following code after macroexpand-1:

(clojure.core/or (clojure.core/and ?c #"trip") (clojure.core/and ?c
#"Trip") (clojure.core/and ?c #"Grace") (clojure.core/and ?c
#"grace"))

Which is exactly what I want.  However, when I use it in the context I
want, it goes too far and tries to evaluate ?c.  Is there a way to
just have it splice-in the code, or would I need another macro to do
that?

Thanks.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to