Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Clojure's Reference Types
Search
Nate Smith
January 20, 2015
Technology
0
120
Clojure's Reference Types
A short introduction to Clojure's reference types that I gave to the Clojure Toronto meetup.
Nate Smith
January 20, 2015
Tweet
Share
More Decks by Nate Smith
See All by Nate Smith
Building Unbreakable Software
nwjsmith
0
79
Clojure Through Ruby Coloured Glasses
nwjsmith
1
170
Refactoring to Middleware
nwjsmith
2
220
CAP Theorem
nwjsmith
2
150
Other Decks in Technology
See All in Technology
Digitization部 紹介資料
sansan33
PRO
1
5.5k
BI ツールはもういらない?Amazon RedShift & MCP Server で試みる新しいデータ分析アプローチ
cdataj
0
160
20251007: What happens when multi-agent systems become larger? (CyberAgent, Inc)
ornew
1
270
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
2.8k
プロポーザルのコツ ~ Kaigi on Rails 2025 初参加で3名の登壇を実現 ~
naro143
1
240
[Codex Meetup Japan #1] Codex-Powered Mobile Apps Development
korodroid
2
720
20201008_ファインディ_品質意識を育てる役目は人かAIか___2_.pdf
findy_eventslides
2
640
Introduction to Sansan Meishi Maker Development Engineer
sansan33
PRO
0
310
AI時代こそ求められる設計力- AWSクラウドデザインパターン3選で信頼性と拡張性を高める-
kenichirokimura
3
310
新規事業におけるGORM+SQLx併用アーキテクチャ
hacomono
PRO
0
250
アイテムレビュー機能導入からの学びと改善
zozotech
PRO
0
150
ユーザーの声とAI検証で進める、プロダクトディスカバリー
sansantech
PRO
1
140
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1032
470k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
What's in a price? How to price your products and services
michaelherold
246
12k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
20
1.2k
Testing 201, or: Great Expectations
jmmastey
45
7.7k
How GitHub (no longer) Works
holman
315
140k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
The Cult of Friendly URLs
andyhume
79
6.6k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.6k
The Language of Interfaces
destraynor
162
25k
Rails Girls Zürich Keynote
gr2m
95
14k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.5k
Transcript
NATE SMITH @nwjsmith theinternate.com
Clojure's REFERENCE TYPES
ATOMS AGENTS REFS
VARS
(def wayne-gretzky {:number 99 :position "C" :goals 45}) wayne-gretzky ;;
=> {:number 99 :position "C" :goals 45}
wayne_gretzky = { number: 99, position: "C", goals: 45 }
(def wayne-gretzky {:number 99 :position "C" :goals 45}) (defn score
[player] (update-in player [:goals] inc))
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} (score
wayne-gretzky) ;; => {:number 99 :position "C" :goals 46} wayne-gretzky ;; => {:number 99 :position "C" :goals 45}
IDENTITY and STATE
None
None
None
None
IDENTITY ≠ STATE
None
None
BOTH MICHAEL JACKSON
WHAT CHANGED?
HIS IDENTITY
STATE IS IMMUTABLE, IDENTITIES CHANGE
wayne_gretzky = { number: 99, position: "C", goals: 45 }
wayne_gretzky[:goals] += 1 wayne_gretzky # => { number: 99, position: "C", goals: 46 }
ATOMS AGENTS REFS
ATOMS
(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}
(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}
AGENTS
(def agent-gretzky (agent {:position "C" :number 99 :goals 0})) @agent-gretzky
;; => {:position "C" :number 99 :goals 0}
(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}
REFS
THEY'RE FRIGGIN' MAGIC
(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}
(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}
Atoms: independent, synchronous Agents: independent, asynchronous Refs: coordinated, synchronous
THANKS!
None