Clojure Koans 17 Atoms Notebook

{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
        ;; complient is used for autocompletion
        ;; add your libs here (and restart the runtime to pick up changes)
        compliment/compliment {:mvn/version "0.3.9"}}}
deps.edn
Extensible Data Notation
{:hello (clojure-version)}
0.1s
Clojure

Initial Thoughts

Atoms and references definitely seem similar. What are their differentiating characteristics and use cases?

(def atomic-clock (atom 0))
Clojure
;; "Atoms are like refs"
(= 0 @atomic-clock)
;; question 17001: What are the similarities and differences between atoms and refs?
;; question 17002: How do atoms in Clojure compare with variables in other languages?
;; skill 17001: Create an atom and bind it to memory (?) by using `atom` and `def`
;; skill 17002: Access the value stored in an atom (?) by dereferencing (?) it with the `@` prefix/shorthand (?)
Clojure
;; "You can change at the swap meet"
(= 1 (do
         (swap! atomic-clock inc)
         @atomic-clock))
;; skill 17003: Update/modify (?) an atom by using the `swap!` macro/function (?)
Clojure
;; "Keep taxes out of this: swapping requires no transaction"
(= 5 (do
         (swap! atomic-clock + 4)
         @atomic-clock))
;; skill 17004: Update/modify (?) an atom by using the `swap!` macro/function (?) and an argument
Clojure
;; "Any number of arguments might happen during a swap"
(= 20 (do
          (swap! atomic-clock + 1 2 3 4 5)
          @atomic-clock))
;; skill 17005: Update/modify (?) an atom by using the `swap!` macro/function (?) and multiple arguments
Clojure
;; "Atomic atoms are atomic"
(= 20 (do
          (compare-and-set! atomic-clock 100 :fin)
          @atomic-clock))
;; description: This Koan compares atomic-clock (which is currently 20) with 100, and since this comparison is false, atomic-clock is not changed to `:fin`. 
;; skill 17006: Check to see if an atom has a certain value, and, if so, change its value by using the `compare-and-set!` macro/function (?)
Clojure
;; "When your expectations are aligned with reality, things proceed that way"
(= :fin (do
            (compare-and-set! atomic-clock 20 :fin)
            @atomic-clock))
Clojure

Closing Thoughts

The Koans have been helpful in my learnings of Clojure as a springboard deeper into the inner workings of the language. I have to remind myself that it's OK if it doesn't all make sense right this very moment. My experience of going through the Clojure Koans has been fantastic for introspection into the educational experiences I'd like to create for myself and others.

Runtimes (1)