I've got a few namespaces that use similar functions, but with slight
variations.  I was thinking of using a macro to generate the
repetitive bits of this code, but I'm just not able to get past Cons
exceptions and missing symbols.

Here's a simple version of what I'm trying to do.  Right now, I have a
bunch of namespaces that have something like this at the top:

    (def ^:const TYPE "user")
    (defn index [& [property value]] (db/get-index TYPE property
value))
    (defn find-user [property value] (index property value))
    (defn by-login [value] (find-user :login value))
    (defn by-email [value] (find-user :email value))

What I'd like to do is something like this:

    (defmacro defnode
      [type-name & settings]
      `(do (def (symbol "TYPE") (str ~type-name))
        (defn (symbol "index") [] (db/get-index (symbol "TYPE")))
        (defn (symbol (str "find-" ~type-name)) [property# value#]
((symbol "index") property# value#))
        (doseq [property# (apply hash-map settings)]
          (defn (symbol (str "by-" (name property#))) [value#]
((symbol (str "find-" ~type-name)) value#)))))

(Obviously this code is wrong--it's sort of Clojure pseudo-code--but
hopefully you get the gist).

So that all that code previously becomes:

    (defnode user
      :index-by [:login :email])

And when the app is compiled, I should be able to do something like:

    user=> user/TYPE
    "user"
    user=> (user/index)
    #<SearchIndex LuceneIndex$SearchIndex@7e270dad>
    user=> (user/by-login "Bob")
    {:name "Bob" :password "xyz" :created "Oct. 10, 2011"}

I've tried using intern rather than the macro templates, but I'm not
sure that's working either.  My biggest problem is dereferencing the
symbols I've created previously.  For example, if I create the var for
TYPE in the namespace, how do I reference that in my functions?  Or
once I create the "find-?" function, how do I call that from the
subsequent (by-*) functions?

Or am I just asking too much from the macros?

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

Reply via email to