16. Atoms - Koans Walkthrough

This video introduces atoms, which are like refs, but provide swap! and compare-and-set! to mutate an atom's value without having to use a dosync transaction.

Here is the 16_atoms.clj source code:

(def atomic-clock (atom 0))

  "Atoms are like refs"
  (= __ @atomic-clock)

  "You can change at the swap meet"
  (= __ (do
          (swap! atomic-clock inc)

  "Keep taxes out of this: swapping requires no transaction"
  (= 5 (do

  "Any number of arguments might happen during a swap"
  (= __ (do
          (swap! atomic-clock + 1 2 3 4 5)

  "Atomic atoms are atomic"
  (= __ (do
          (compare-and-set! atomic-clock 100 :fin)

  "When your expectations are aligned with reality things, proceed that way"
  (= :fin (do
            (compare-and-set! __ __ __)

Clojure Koans Walkthrough in Light Table IDE

This screencast tutorial helps you learn the Clojure programming language. Experience the joy of Clojure in the Light Table IDE as we tour through the Clojure Koans, taking you all the way from Beginner to Intermediate to Advanced.

Clojure is a Lisp created by Rich Hickey that runs on the JVM, as an alternative to Java. ClojureScript can target the web browser environment, and node.js, by compiling down to JavaScript, using the Google Closure compiler. Clojure features immutability, functional programming, and being a Lisp, macros.