I wrote this little macro today that lets you do it. However, I feel like
it was implemented in a pattern match library or something... but I'm not
sure.
Someone let me know if I'm re-inventing the wheel.
If I'm not, here's the code:
(defn re-bind-fn
[str re bnds act & forms]
`(let [~'__TEMP__ (re-find ~re ~str)
~bnds ~'__TEMP__]
(if ~'__TEMP__
~act
~(if (empty? forms)
nil
(apply re-bind-fn str forms)))))
(defmacro re-bind
[str re bnds act & forms]
(apply re-bind-fn str re bnds act forms))
You can do things like this:
(defn msg-parse
[s]
(re-bind
s
#"HELLO (\w+)" [_ nme] (println "Hello message for " nme)
#"COORDS (\d+) (\d+)" [_ x y] (println "Coords: (" x ", " y ")")
#".*" _ (println "unimportant")))
cons-irc.util> (msg-parse "COORDS 123 456")
Coords: ( 123 , 456 )
nil
cons-irc.util> (msg-parse "HELLO Kaylen")
Hello message for Kaylen
nil
cons-irc.util> (msg-parse "fdsfsd")
unimportant
nil
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.