State and Identity

Thinking out loud here. I’ve been rededicating some of my attention to Clojure lately with some basic success. However, coming from a background of object oriented languages focused on imperative programming and mutable state, I’m having trouble really internalizing the concepts in Clojure. I recently read Rich Hickey’s essay Values and Change – Clojure’s Approach to Identity and State and while I understand it from a very high level, the details seem to escape me in some significant way when I think about writing programs in a functional style using mostly immutable data.

The real problem is that I don’t completely understand what I’m not understanding. There is just a fuzzy, nagging feeling in the back of my brain that says “This can never work”, examples to the contrary notwithstanding. As a C# developer, I’m used to just modifying anything as necessary in my programs, adding values to lists, modifying dictionaries, randomly changing object values just to screw with people. Ok, maybe not that last part. But with Clojure, the world is very, very different.

[clojure]Last login: Tue May 4 16:42:01 on ttys000
Bretts-Mac-Pro:~ admin$ clj
Clojure 1.1.0
user=> (def mylist ‘(1 2 3))
user=> mylist
(1 2 3)
user=> (cons 4 mylist)
(4 1 2 3)
user=> mylist
(1 2 3)

Here, I create a list, show the list at the command line, cons another number to the list and then show that the original list is unchanged. This takes some getting used to. Rich recommends the following when coming from an OO language:

In coming to Clojure from an OO language, you can use one of its persistent collections, e.g. maps, instead of objects. Use values as much as possible. And for those cases where your objects are truly modeling identities (far fewer cases than you might realize until you start thinking about it this way), you can use a Ref or Agent with e.g. a map as its state in order to model an identity with changing state.

Conceptually, I can think of a map of maps to model the world but I’ll be damned if I can really accept it right now. I understand the benefits behind the immutable data of Clojure but I look at my day to day programming tasks and just don’t see where it’s important. Maybe that’s just a failure of my imagination. I’m continuing to plug away on learning Clojure, trying to work my way into the Concurrency chapter of Stuart Halloway’s Programming Clojure which has been excellent.

2 comments on “State and Identity

  1. been there. a word of advice: don’t think so much, and just write some programs. you are stepping into a world that is a lot simpler than the one you are coming from, so it feels a bit strange and you are wondering what you should be doing. where’s the all the ceremony? how do i structure my programs?

    it’s functional programming, so just write functions and don’t worry so much about modeling what the world *is*, and think more about what the world *does*. start with your main function and work from there. chances are you won’t even have to touch the concurrency primitives for most tasks.

  2. Niklaus Wirth's Ghost

    May 12, 2010 at 10:09 am


    That’s kind of the solution I was slowly coming around to so it helps to hear someone else say that. I’m writing smaller functions and more of them, trying to utilize the sequence library as much as possible. When I do that, the programs sort of write themselves, as much as they can based on my extremely novice ability.

Comments are closed.