Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create mutable state inside Clojure records?

Tags:

clojure

I am considering using Clojure records to map to changing entities in my program. Are they mutable? Or do you need to use extra refs within the records? I am a little confused about this

like image 800
yazz.com Avatar asked Apr 05 '11 15:04

yazz.com


1 Answers

It's well worth watching Rich Hickey's fantastic video on identity and state.

Records are designed to be immutable and store the state of something as a value.

To model the state of a changing entity, I'd recommend using a ref that refers to an immutable value that represents the current state. You can use records for the immutable state, but often it's simpler just to use a simple map.

A simple example, where the mutable state is a scoreboard for a game:

; set up map of current scores for each player
(def scores 
  (ref 
    {:mary 0 
     :joe  0   }))

; create a function that increments scores as a side effect
(defn add-score [player amount]
  (dosync
    (alter scores update-in [player] + amount)))

; add some scores
(add-score :mary (rand-int 10))
(add-score :joe (rand-int 10))

; read the scores
@scores
=> {:mary 6, :joe 1}
like image 164
mikera Avatar answered Oct 21 '22 12:10

mikera