Hey David,
> I guess its got something to do with the fact that match itself is a macro?
Correct. The match macro just takes the list `(!nil? a)`, which isn't
a valid match form, so it complains about it.
> Is there some other way that I could extend match to only match when the
> variable is not nil?
Yep! You just need to replace the match macro itself, rather than
trying to change the rows.
Here's my go at it:
(def not-nil? (complement nil?))
(defmacro match-nn [expr & matches]
(let [add-guard (fn [e]
(if (symbol? e)
(list e :guard `not-nil?)
e))
guard-row (fn [[cond result]]
(if (vector? cond)
[(vec (map add-guard cond)) result]
[cond result]))
rows (partition 2 matches)
new-rows (mapcat guard-row rows)]
`(match ~expr
~@new-rows)))
(defmacro match-pred [expr & matches]
(let [add-guard (fn [e]
(if (and (seq? e)
(not= (second e) :guard))
(list (second e) :guard (first e))
e))
guard-row (fn [[cond result]]
(if (vector? cond)
[(vec (map add-guard cond)) result]
[cond result]))
rows (partition 2 matches)
new-rows (mapcat guard-row rows)]
`(match ~expr
~@new-rows)))
`match-nn` will make sure all matched variables satisfy the `not-nil?`
predicate. `match-pred` will allow you to write guards like `(not-nil?
a)` and have it translated to `(a :guard not-nil?)`.
I hope that helps!
--
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