Hello folks,

I've been playing a little with core.match and thought it would be fun to
make a defmatch macro:

https://gist.github.com/1258277

The super simple macro I came up with enables this:

(defmacro defmatch [name bindings & patterns]
   `(defn ~name ~bindings
     (match ~bindings
     ~@patterns)))

(defmatch len [coll]
   [[]] 0
   [[fst & rest]] (inc (len rest)))

(fact
   (len [\a \b \c]) => 3)

What I'd like is to be able to define len like this:

(defmatch len
   [[]] 0
   [[fst & rest]] (inc (len rest)))

This macro is moving in the right direction:

(defmacro defmatch [name & patterns]
  "macro to make match-based functions"
    `(defn ~name [abc#]
      (match [abc#]
      ~@patterns)))

To do it i seems like I would need to generate n local bindings where n is
the length of pattern, so in this case it would be 1.

So something like this:

(defn- make-n-bindings [n]
  ;; not sure :)
)

(defmacro defmatch [name & patterns]
   `(defn ~name ~(make-n-bindings (count (first patterns)))
     (match ~(make-n-bindings (count (first patterns)))
     ~@patterns)))

Any clues on how to make this work?

This works great for our len function (because the length of the bindings is
hardcoded).

Alex

-- 
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