Clojure's Reference Types

6539ae86c7d8ddec66f3928beddad569?s=47 Nate Smith
January 20, 2015

Clojure's Reference Types

A short introduction to Clojure's reference types that I gave to the Clojure Toronto meetup.

6539ae86c7d8ddec66f3928beddad569?s=128

Nate Smith

January 20, 2015
Tweet

Transcript

  1. NATE SMITH @nwjsmith theinternate.com

  2. Clojure's REFERENCE TYPES

  3. ATOMS AGENTS REFS

  4. VARS

  5. (def wayne-gretzky {:number 99 :position "C" :goals 45}) wayne-gretzky ;;

    => {:number 99 :position "C" :goals 45}
  6. wayne_gretzky = { number: 99, position: "C", goals: 45 }

  7. (def wayne-gretzky {:number 99 :position "C" :goals 45}) (defn score

    [player] (update-in player [:goals] inc))
  8. wayne-gretzky ;; => {:number 99 :position "C" :goals 45} (score

    wayne-gretzky) ;; => {:number 99 :position "C" :goals 46}
  9. wayne-gretzky ;; => {:number 99 :position "C" :goals 45} (score

    wayne-gretzky) ;; => {:number 99 :position "C" :goals 46} wayne-gretzky ;; => {:number 99 :position "C" :goals 45}
  10. IDENTITY and STATE

  11. None
  12. None
  13. None
  14. None
  15. IDENTITY ≠ STATE

  16. None
  17. None
  18. BOTH MICHAEL JACKSON

  19. WHAT CHANGED?

  20. HIS IDENTITY

  21. STATE IS IMMUTABLE, IDENTITIES CHANGE

  22. wayne_gretzky = { number: 99, position: "C", goals: 45 }

    wayne_gretzky[:goals] += 1 wayne_gretzky # => { number: 99, position: "C", goals: 46 }
  23. ATOMS AGENTS REFS

  24. ATOMS

  25. (def atomic-wayne-gretzky (atom {:position "C" :number 99 :goals 0})) (deref

    atomic-wayne-gretzky) ;; => {:position "C" :number 99 :goals 0} @atomic-wayne-gretzky ;; => {:position "C" :number 99 :goals 0}
  26. (def atomic-wayne-gretzky (atom {:position "C" :number 99 :goals 0})) (dothreads!

    (fn [] (swap! atomic-wayne-gretzky score)) :threads 100 :times 100) @atomic-wayne-gretzky ;; => {:position "C" :number 99 :goals 10000}
  27. AGENTS

  28. (def agent-gretzky (agent {:position "C" :number 99 :goals 0})) @agent-gretzky

    ;; => {:position "C" :number 99 :goals 0}
  29. (defn score-slowly [player] (Thread/sleep 5000) (update-in player [:goals] inc)) (send

    agent-gretzky score-slowly) @agent-gretzky ;; => {:position "C" :number 99 :goals 0} @agent-gretzky ;; => {:position "C" :number 99 :goals 0} ;; 5 seconds later... @agent-gretzky ;; => {:position "C" :number 99 :goals 1}
  30. REFS

  31. THEY'RE FRIGGIN' MAGIC

  32. (def gretzky-curr (ref {:position "C" :number 99 :goals 0})) (def

    gretzky-prev (ref @gretzky-curr)) @gretzky-curr ;; => {:position "C" :number 99 :goals 0} @gretzky-prev ;; => {:position "C" :number 99 :goals 0}
  33. (dothreads! (fn [] (dosync (ref-set gretzky-prev @gretzky-curr) (alter gretzky-curr score)))

    :threads 100 :times 100) @gretzky-prev ;; => {:position "C" :number 99 :goals 9999} @gretzky-curr ;; => {:position "C" :number 99 :goals 10000}
  34. Atoms: independent, synchronous Agents: independent, asynchronous Refs: coordinated, synchronous

  35. THANKS!

  36. None