On Wed, Aug 3, 2011 at 5: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 > I'm biased but I think core.logic is a pretty good way to do this kind of thing. (ns clojure.core.logic.db (:refer-clojure :exclude [inc reify ==]) (:use [clojure.core.logic minikanren prelude])) (defrel category ^:index id ^:index name) (facts category [[1 'idea] [2 'todo]]) (defrel notes ^:index id ^:index category ^:index text) (facts notes [[1 1 "My cool widget"] [2 1 "My cool Clojure project"] [3 2 "Walk the dog"]]) (defn notes-for-category [name out] (exist [cid nid t] (category cid name) (notes nid cid t) (== [name t] out))) (comment (run* [q] (notes-for-category 'idea q)) ;; ([idea "My cool widget"] [idea "My cool Clojure project"]) ;; rename a category (fact category 1 'thought) (run* [q] (notes-for-category 'thought q)) ;; ([thought "My cool widget"] [thought "My cool Clojure project"]) ) In order to flesh out this use case I'd have to hear a lot more feedback about what people would like to see. David -- 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