I like to have the datastructure as one big mutable structure. It's not optimal in many cases, but it is simple. In this case you could have something like:
(def NoteDb (atom [{:text "a note" :category :misc} {:text "note 2" :category :misc} {:text "blabla" :category :important}])) (defn get-category-notes [category] (filter #(= (:category %) category) @NoteDb)) (defn update-category [category] (let [notes (get-category-notes category)] .. do stuff .. )) (defn update-note-text [index text] (swap! NoteDb assoc [index :text] text) (update-category (:category (nth @NoteDb index)))) I'm guessing when you say "And when I change the name of a category, all notes belonging to that category know that the category name has changed.", you mean that each note is some sort of object, which does some sort of operation when that category changed. Instead you could have a update function that operates on notes from a specific category. I also think that you should think over whether it is really necessary with mutable notes. Depending on the case you're modeling, it could suffice with some sort of function that updates the data and recurs. --- The bottom line is that instead of having multiple nodes that are objects that take care of themselves, you can have functions operating on the full data structure. Jonathan On Wed, Aug 3, 2011 at 11:26 AM, Rickard Lindberg <ricl...@gmail.com> wrote: > Hi, > > I am interested in Clojures approach to managing state and its use of > immutable > values. I believe immutable values will make the life of programmers easier > and > I'm trying to figure out how I can simplify my OO code by using more > immutable > values. > > In particular, I am wondering how I can model notes that can belong to a > category. > > So a note is a piece of text and a reference to a category. A category is > just > a name. However, both notes and categories are entities (the identity is > *not* > defined by their value). So it is perfectly ok to have two notes with the > same > text and same category, yet they are different notes. > > The data structure holding these things together (lets call it NoteDb) has > a > list of categories and a list of notes. And I want to be able to say things > like change the text of a note to this. And when I change the name of a > category, all notes belonging to that category know that the category name > has > changed. I also want to be able to get all notes belonging to a category. > > Should I model this as a list of refs? Is there another way to think about > this > problem that I don't see because I have mainly done work in OO languages. > > -- > Rickard Lindberg > > -- > 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 -- 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