Hi,

Earlier Stuart Sierra replied as follows:

Hi Patrick,

Here's one way to do it:

(defn new-person [name]
  (ref {:name name, :friends #{}}))

(defn are-friends [a b]
  (dosync
   (commute a assoc :friends (conj (:friends @a) b))
   (commute b assoc :friends (conj (:friends @b) a))))

(def bill (new-person "Bill"))
(def bob (new-person "Bob"))



... so I tried the followings:

user=> (defn person [name]
 (ref {:name name :friends #{}}))
#'user/person
user=>
(def bill (person "Bill"))
#'user/bill
user=>
(def bob (person "Bob"))
#'user/bob
user=>
(dosync
  (commute bill assoc :friends (conj (:friends @bill) bob))
)
{:friends #{#<Ref clojure.lang....@17414c8>}, :name "Bill"}
user=> (:friends @bill)
#{#<Ref clojure.lang....@17414c8>}

Adding another friend for bill
(def beck (person "Becky"))
and
(dosync
  (commute bill assoc :friends (conj (:friends @bill) beck))
)
Now I want to print list of names of friends of Bill.
(count (:friends @bill)) returns 2, which is working.
Tried  (:name (first (:friends @bill)) but returned nil.
I expected a name gets printed.

thanks in advance.
-sun



--~--~---------~--~----~------------~-------~--~----~
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
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