On Tue, Apr 6, 2010 at 3:43 PM, Douglas Philips <[email protected]> wrote:
> (def john-doe {:name "John Doe" :email "[email protected]"})
>
> (def account {:identity john-doe :balance 100} )
>
> (assoc john-doe :email "john.doe at gee mail.com")
>
> Now the account contains old/obsolete data and no cycles are needed to
> cause that.
>
> Doesn't seem that cycles are the problem...
The cycles are gone but the identity john-doe aand its curren-state are
still conflated so you get the same problem:
(def account {:identity john-doe :balance 100} )
(assoc john-doe :email "john.doe at gee mail.com")
On the first line you define a reference between the current value of
account and the current value of john-doe, so it is to be expected that
(assoc john-doe :email "john.doe at gee mail.com") won't give the expected
result since you didn't provided the system with enough knowledge.
(def account {:identity #'john-doe :balance 100} )
(def john-doe (assoc john-doe :email "john.doe at gee mail.com"))
; or (alter-var-root #'john-doe assoc :email "john.doe at gee mail.com")
there account points to the updated john-doe
Of course, one should not abuse vars, and the above code should at least be
rewritten with refs:
(def account (ref {:identity john-doe :balance 100}) )
(def john-doe (ref {:name "John Doe" :email "[email protected]"}))
(dosync (alter john-doe assoc :email "john.doe at gee mail.com"))
Really, to me the problem isn't creating cyclic data-structures but updating
them: the system must know which mutual references are to the identity (and
should be updated) and which are not. You have to give this information to
the system one way or the other, the system can't guess.
Btw, to some extent, one can create cyclic data-structures in Clojure (only
lazyseqs though -- unless you implement your own map or use lazy-map etc.) :
user=> (defn cyclic-seq [coll] (let [s (promise)] @(deliver s (lazy-cat coll
@s))))
user=> (let [s (cyclic-seq [1 2 3])] (identical? (seq s) (seq (drop 3 s))))
true
Christophe
--
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
To unsubscribe, reply using "remove me" as the subject.