On Sat, 13 Nov 2010 06:31:03 -0800, garf wrote: > If I have a struct whose creation will require some function calls that > references some of its members, I am unsure how to go about it in a > clean way. For example: > (defstruct tz :a :b :c) > > (def tz1 1 2 (+ (:a tz1) (:b tz1)) > Could you give a more realistic example of how you plan to use this?
If the only restriction is that you only know the values for :a and :b at the time you want to create the structure, then this would work: (defn create-tz [a-val b-val] (struct-map tz :a a-val :b b-val :c (some-fn a-val b-val))) > will not work, but reflects the problem. Ideally at the end of this I > could do (:c tz1) and get 3 back. Any suggestions? Originally I had > used assocs, but that runs into the immutability problem, for example > > (defstruct tz :a :b) > (def tz1 1 2) > (assoc tz1 :c (+ (:a tz1) (:b tz1)) > > does not actually update tz1 > Correct. Values are immutable, and in Clojure, even collections are values! If you want to have a variable hold a reference to some changing values then what you want is to use an atom or a ref. Atom is more easy to use, and unless you have several mutable values that need concurrent updating, is sufficient, so here's an example: > (def tz1 (atom (struct-map tz :a 1 :b 2))) #'user/tz1 > @tz1 {:a 1, :b 2, :c nil} > (swap! tz1 #(assoc % :c (+ (:a %) (:b %)))) {:a 1, :b 2, :c 3} > @tz1 {:a 1, :b 2, :c 3} Hope that helps, -- Michel Alexandre Salim Clojure contributor: http://clojure.org/contributing GPG key ID: 78884778 µblog: http://identi.ca/hircus | Jabber: hir...@jabber.ccc.de http://twitter.com/hircus | IRC: hir...@irc.freenode.net () ascii ribbon campaign - against html e-mail /\ www.asciiribbon.org - against proprietary attachments -- 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