thanks Michel, the more realistic form was quite a bit longer, but i
will show just a piece of my (mis)direction

(defstruct Ruleform-struct
  :rname :rule-seq
      :if-cnt :then-cnt )

(defn rule-if-names [{:keys [ rule-seq if-cnt ] :as ruleform } ]
  ; (Camoflage Spotted Cover  Action  Use-Cover)
  ;    ==> (Camoflage Spotted Cover)
  (assoc ruleform :if-names
    (take if-cnt rule-seq))   )
:
:
(defn rule-col-multiply [{:keys [ col-vectors  ] :as ruleform } ]
  ; determine necessary increment for col counts to insure unique
lookup sums
  ; column unique items: [ 4  3  8 ]  --index increments-->  [1  4
12]
  (assoc ruleform :col-multiply
  (->> (reductions * (map count col-vectors))
          (cons 1)
          (drop-last))))

so basically I was trying to add about 7 additional attributes after
the initial creation.  This was a part of a fuzzy state/rule engine
that was compiled for high speed, thought I would port from my
original Smalltalk version to Clojure.  Well 'port' is the wrong word,
rewrite in a functional manner.   I should use defrecord, but it gave
me fits first time I tried, so am keeping it simple for now.

thanks for the input & any further advice


On Nov 13, 10:48 am, Michel Alexandre Salim <michel
+...@michelsylvain.info> wrote:
> 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

Reply via email to