How about; (defn get-accessors "Given a struct definition, return a map of its keys and generated accessors for each key." [the-struct] (let [st-kys (keys (struct the-struct))] (reduce (fn [accsrs ky] (assoc accsrs ky (accessor the-struct ky))) {} st-kys)))
(defstruct test-s :a :b :c) (def acc (get-accessors test-s)) ((acc :a) (struct test-s 5 3 2)) 5 Obviously looking up the accessor to look up the value defeats the performance objective, but not if you get the accessors once, "let" the needed ones and then use them in a loop processing lots of strucs say. On Tue, Jun 30, 2009 at 6:14 AM, samppi <rbysam...@gmail.com> wrote: > > Yes, but I'm not so sure that you can get usable accessors with those > keys: > Clojure 1.0.0- > user=> (defstruct test-s :a :b :c) > #'user/test-s > user=> (defstruct test-2-s :a :b :c) > #'user/test-2-s > user=> (def accessor-a (accessor test-s :a)) > #'user/accessor-a > user=> (accessor-a (struct test-2-s 5 3 2)) > java.lang.Exception: Accessor/struct mismatch (NO_SOURCE_FILE:0) > > But thanks for the tip anyway! > > On Jun 29, 6:47 pm, Adrian Cuthbertson <adrian.cuthbert...@gmail.com> > wrote: > > As a matter of interest, one can get the keys keys in an unknown struct > by > > allocating an empty struct; > > > > (def st (create-struct :a :b :c)) > > (keys (struct st)) > > (:a :b :c) > > > > -Adrian. > > > > > > > > On Tue, Jun 30, 2009 at 12:14 AM, samppi <rbysam...@gmail.com> wrote: > > > > > Wonderful. Thanks for the answer. > > > > > On Jun 29, 2:47 pm, Rich Hickey <richhic...@gmail.com> wrote: > > > > On Jun 29, 4:59 pm, samppi <rbysam...@gmail.com> wrote: > > > > > > > clojure.xml/parse returns a PersistentStructMap. Is there a way to > > > > > refer to its struct template? I wish to create accessors for its > keys, > > > > > such as :tag, :attrs, and :content, with the accessor function for > > > > > speed. > > > > > > If you look at the top of xml.clj you'll see they already exist. > > > > > > Rich > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---