Jay Fields is a software developer at DRW Trading. He has a passion for discovering and maturing innovative solutions. His most recent work has been in the Domain Specific Language space where he's delivered applications that empowered subject matter experts to write the business rules of the applications. He is also very interested in maturing software design through software testing. Jay is a DZone MVB and is not an employee of DZone and has posted 115 posts at DZone. You can read more from them at their website. View Full User Profile

Clojure: State Management

  • submit to reddit


Clearly, state management in Clojure is greatly misunderstood.

I actually had a hard time not calling this blog entry "Clojure, it's about state". I think state shapes Clojure more than any other influence; it's the core of the language (as far as I can tell). Rich Hickey has clearly spent a lot of time thinking about state - there's an essay at http://clojure.org/state which describes common problems with a traditional approach to state management and Clojure's solutions.


At the end of Rich's essay he says:

result.add(nums[i] * 2);
(2 4 6)

user=> (map double-int the-array)
(2 4 6)

(2 4 6)
but, I digress.

  result += nums[i];
user=> (reduce + [1 2 3])


user=> @apple-price
user=> (update-apple-price 300.00)
user=> @apple-price
user=> (update-apple-price 301.00)
user=> (update-apple-price 302.00)
user=> @apple-price               


You may remember the following example from Java Concurrency in Practice.
public class UnsafeSequence {
    private int value;

     * Returns a unique value.
    public int getNext() {
        return value++;
The book explains why this could cause potential problems.
user=> (get-next)
user=> (get-next)


(import '(java.util.concurrent Executors)) 

(def *pool* (Executors/newFixedThreadPool

  (dotimes [t thread-count]

(def ticks (atom 0)) 
(defn tick [] (swap! ticks inc)) 
(dothreads tick :threads 1000 :times 100) 
@ticks ;=> 100000


user=> (defn cancel-order [id]          
  (<a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/commute">commute</a> active-orders <a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/disj">disj</a> id)
  (commute cancelled-orders <a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/conj">conj</a> id)))
user=> (cancel-order 2)
#{1 2}
user=> @active-orders
#{3 4}
user=> @cancelled-orders
#{1 2}
As you can see from the example, we're moving an order id from active to cancelled. Again, our REPL session doesn't show the power of what's going on with a ref, but clojure.org/refs contains a good explanation -

user=> (def uid (ref 0))
user=> (defn use-id []                 
  (commute uid inc)             
  (commute used-id conj @uid))) 
user=> @used-id
user=> (def uid (ref 0))                       
user=> (defn use-id []                         
  (alter uid inc)              
  (alter used-id conj @uid)))  
user=> @used-id                                

Last, but not least, agents are also available. From clojure.org/agents -

  • Clojure has plenty of support for managing state
  • Clojure, it's about state.


From http://blog.jayfields.com/2011/04/clojure-state-management.html

Published at DZone with permission of Jay Fields, author and DZone MVB.




Simon Bruner replied on Wed, 2011/04/13 - 3:52am

Hi Jay, great article. There is a problem with the first commute code example, looks like the hypertext links have invaded your code. Clojure is *absolutely* my next language, the state management approach appears to be revolutionary ( although database developers must be shaking their heads knowingly ). Will we look back in 10 years and wonder how developers worked without software transactional memory (STM) support baked into their platform? ( Much like how we look back at platforms that don't have garbage collection now I would imagine! ).

Damien Lepage replied on Wed, 2011/04/13 - 9:16am

Great article. Thanks for all your Clojure evangelisation on DZone, that's awesome. I personally started playing with Clojure a few months ago in my spare time and I'm hoping I'll be able to use it at work soon. It just feels so refreshing.

Liam Knox replied on Wed, 2011/04/13 - 8:14pm


Read it. What the hell does this mean? State? Without context this sentence is absolutely meaningless. Please pay atention and express what you mean.

Great article? Obviously others can't read also. Clojure... please ...

Simon Bruner replied on Wed, 2011/04/13 - 10:58pm in response to: Liam Knox

Hi Liam,

I guess you're not familiar with terminology commonly used in computer science. Here's a definition of state for you for future reference, it should assist you in understanding what computer scientists and information technology workers are referring to:

State is a unique configuration of information in a program or machine.

So with the definition out of the way, the phrase you have quoted should now make sense.

But to spell it out - the state of an application is expressed by the value of variables. This article explains how clojure manages changes to the value of variables. In most mainstream languages\platforms, this change isn't managed at all - it just happens. In clojure, changing the value of a variable must be performed atomically. I don't know clojure personally, but that sounds like a really good idea to me - and most database vendors would agree with me.

I'd mention your mispelling of attention - but that would be unreasonably picky of me :)

Shoaib Almas replied on Sat, 2012/08/25 - 5:53am

I appreciate your writing on Clojure. Please keep at it.



Java Forum

Comment viewing options
