Ref to map vs. map to refs vs. multiple refs

Posted by mikera on Stack Overflow See other posts from Stack Overflow or by mikera
Published on 2010-06-16T20:11:12Z Indexed on 2010/06/16 20:52 UTC
Read the original article Hit count: 253

I'm working on a GUI application in Swing+Clojure that requires various mutable pieces of data (e.g. scroll position, user data, filename, selected tool options etc.).

I can see at least three different ways of handling this set of data:

Create a ref to a map of all the data:

(def data (ref {
  :filename    "filename.xml" 
  :scroll      [0 0] }))

Create a map of refs to the individual data elements:

(def datamap {
  :filename    (ref "filename.xml") 
  :scroll      (ref [0 0]) }))

Create a separate ref for each in the namespace:

(def scroll (ref [0 0]))    

(def filename (ref "filename.xml"))

Note: This data will be accessed concurrently, e.g. by background processing threads or the Swing event handling thread. However there probably isn't a need for consistent transactional updates of multiple elements.

What would be your recommended approach and why?

© Stack Overflow or respective owner

Related posts about java

Related posts about data-structures