title: Clojure: Idiomatic use of atom and ref?
body: <p>I am working out a bit of Clojure code that will take a ref to a map and increment a key value pair in the map.  I think I am using ref correctly, but Im not sure about atom.  Do I need to use swap! to be more idiomatic?  I am new to STM and Clojure, does this look thread-safe / sane?  What am I missing?</p>
  
  <pre><code>(defn increment-key [ref key]
      (dosync
          (if (= (get @ref key) nil)
              (alter ref assoc key (atom 1))
              (alter ref assoc key (atom (inc @(get @ref key)))))))
  
  (defn -main [&amp; args]
      (def my-map (ref {}))
      (increment-key my-map "yellow")
      (println my-map)
      (increment-key my-map "yellow")
      (println my-map))
  </code></pre>
  
  <p>Prints</p>
  
  <pre><code>$ lein run
  #&lt;Ref@494eaec9: {yellow #&lt;Atom@191410e5: 1&gt;}&gt;
  #&lt;Ref@494eaec9: {yellow #&lt;Atom@7461373f: 2&gt;}&gt;
  </code></pre>