> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
--
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?
Hello. I need to dynamically define records
Were you aware that records support assoc (and as a side-effect,
merge)? You end up with a new record as with all clojure immutable
datastructures, but this is how you can build records up "a piece at a
time".
(defn complicated-function [params]
(let [p (Person. nil nil)
parammap (zipmap [:name :age] params)]
(merge p parammap)))
user=> (complicated-function ["Aaron" 31])
#:user.Person{:name "aaron", :age 31}
--Aaron
I probably should have clarified that the reason I need concat is that
various functions are returning subsets of the arguments as vectors,
but as stated to keep things simple in the example I just used values
It's possible to do this using reflection, but I don't recommend it.
I think you're probably better off making a map of your tag names to
factory functions, then just applying the factory function to your
parameters.
(def record-factories {"Person #(Person. %1 %2)})
user=>(apply (record-factories "Person") ["Aaron" 31])
#:user.Person{:name "Aaron", :age 31}
Perils of copy-paste to an email window... that's missing a quotation mark.