the never changing face of immutability
TRANSCRIPT
![Page 1: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/1.jpg)
THE NEVER CHANGING FACE OFIMMUTABILITY
CHRIS HOWE-JONES@AGILE_GEEK
15TH DECEMBER 2015
![Page 2: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/2.jpg)
WARNING!!There will be a Lisp!There will be Entomology!There will be History!
![Page 3: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/3.jpg)
THE NEVER CHANGING FACE OF IMMUTABILITY
![Page 4: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/4.jpg)
WHO AM I?
Name: Chris Howe-Jones
Job Title: Technical Navigator
Twitter: @agile_geek
Github: github.com/chrishowejones
Blog: chrishowejones.wordpress.com
![Page 5: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/5.jpg)
CREDENTIALS
28 years of pushing data aroundProcedural/OOP/FPArchitecture & DesignRAD/Agile/LeanCTO
![Page 6: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/6.jpg)
HISTORY LESSON
![Page 7: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/7.jpg)
ONCE UPON A TIME..
Book Keeping
List of entries in a ledger No 'crossing out'!
![Page 8: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/8.jpg)
DAWN OF COMPUTING
MathTransient storage
![Page 9: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/9.jpg)
60'S-90'S
Spot the expense? Memory Tape Disk
![Page 10: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/10.jpg)
21ST CENTURY
Spot the expense?
Developers
Cheap resources: SSD/Disk, Memory, CPU
![Page 11: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/11.jpg)
AND..
![Page 12: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/12.jpg)
IN PLACE COMPUTING
Update data in place Reuse expensive real estate
![Page 13: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/13.jpg)
RDBMS
Data updated Values overwritten Reuse memory and disk
![Page 14: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/14.jpg)
RESULT?In place oriented programming (PLOP) relies on…
![Page 15: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/15.jpg)
MUTATION
![Page 16: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/16.jpg)
WHICH LEADS TO..
![Page 17: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/17.jpg)
COMPLECT
Complecting Identity & Value Especially RDBMS, OOPPessimistic concurrency strategies
![Page 18: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/18.jpg)
WHAT'S CHANGED?
Computing capacity has increased by a million fold!
![Page 19: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/19.jpg)
IMMUTABILITY (AND VALUES) TO THE RESCUE!
![Page 20: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/20.jpg)
VALUES
Values are genericValues are easy to fabricateDrives reuseValues aggregate to valuesDistributable
![Page 21: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/21.jpg)
ISN'T COPYING VALUES INEFFICIENT?
Structural sharingFor example in Clojure:
persistent bit-partitioned vector trie32 node triesWide shallow trees
![Page 22: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/22.jpg)
WHAT DOES IT LOOK LIKE?Immutable by defaultExplicit state changeDatabase as a value
![Page 23: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/23.jpg)
CLOJURESCRIPT ON THE CLIENT(def initial-state {:event {:event/name "" :event/speaker ""} :server-state nil})
(defn- event-form [ui-channel {:keys [event/name event/speaker] :as event}] [:table.table [:tr [:td [:label "Event name:"]] [:td [:input {:type :text :placeholder "Event name..." :defaultValue event/name :on-change (send-value! ui-channel m/->ChangeEventName)}]]] [:tr [:td [:label "Speaker:"]] [:td [:input {:type :text :placeholder "Speaker..." :defaultValue event/speaker :on-change (send-value! ui-channel m/->ChangeEventSpeaker)}]]] [:tr [:td [:button.btn.btn-success {:on-click (send! ui-channel (m/->CreateEvent))} "Go"]]]])
![Page 24: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/24.jpg)
(defrecord ChangeEventName [name])
(defrecord ChangeEventSpeaker [speaker])
(defrecord CreateEvent [event])
(defrecord CreateEventResults [body])
(extend-protocol Message m/ChangeEventName (process-message [{:keys [name]} app] (assoc-in app [:event :event/name] name))) ;; redacted for clarity ...
(extend-protocol EventSource m/CreateEvent (watch-channels [_ {:keys [event] :as app}] #{(rest/create-event event)}))
(extend-protocol Message m/CreateEventResults (process-message [response app] (assoc app :server-state (-> response :body))))
![Page 25: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/25.jpg)
EFFICIENCY
![Page 26: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/26.jpg)
CLOJURE ON THE SERVER(defn- handle-query [db-conn] (fn [{req-body :body-params}] {:body (case (:type req-body) :get-events (data/get-events db-conn) :create-event (data/create-entity db-conn (:txn-data req-body)))}))
(defn app [dbconn] (-> (routes (GET "/" [] home-page) (POST "/q" [] (handle-query dbconn)) (resources "/")) (wrap-restful-format :formats [:edn :transit-json]) (rmd/wrap-defaults (-> rmd/site-defaults (assoc-in [:security :anti-forgery] false)))))
![Page 27: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/27.jpg)
DATOMIC FOR DATA
App get's its own query, comms, memory- Each App is a peer
![Page 28: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/28.jpg)
DATABASE AS A VALUE
Entity Attribute Value Time
Fiona likes Ruby 01/06/2015
Dave likes Haskell 25/09/2015
Fiona likes Clojure 15/12/2015
Effectively DB is localDatalog query language
[:find ?e :where [?e :likes “Clojure”]]
![Page 29: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/29.jpg)
SCHEMA ;;event { :db/id #db/id[:db.part/db] :db/ident :event/name :db/cardinality :db.cardinality/one :db/valueType :db.type/string :db/unique :db.unique/identity :db.install/_attribute :db.part/db } { :db/id #db/id[:db.part/db] :db/ident :event/description :db/cardinality :db.cardinality/one :db/valueType :db.type/string :db.install/_attribute :db.part/db } { :db/id #db/id[:db.part/db] :db/ident :event/location :db/cardinality :db.cardinality/one :db/valueType :db.type/ref :db.install/_attribute :db.part/db } ...
![Page 30: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/30.jpg)
;;location { :db/id #db/id[:db.part/db] :db/ident :location/postCode :db/cardinality :db.cardinality/one :db/valueType :db.type/string :db.install/_attribute :db.part/db } { :db/id #db/id[:db.part/db] :db/ident :location/description :db/cardinality :db.cardinality/one :db/valueType :db.type/string :db.install/_attribute :db.part/db } ...
![Page 31: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/31.jpg)
PERSISTENCE(defn create-entity "Takes transaction data and returns the resolved tempid" [conn tx-data] (let [had-id (contains? tx-data ":db/id") data-with-id (if had-id tx-data (assoc tx-data :db/id #db/id[:db.part/user -1000001])) tx @(d/transact conn [data-with-id])] (if had-id (tx-data ":db/id") (d/resolve-tempid (d/db conn) (:tempids tx) (d/tempid :db.part/user -1000001)))))
(defn get-events [db] (d/pull-many db [:*] (->> (d/q '{:find [?event-id] :where [[?event-id :event/name]]} db) (map first))))
![Page 32: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/32.jpg)
CONCLUSION?
Immutability simplifiesState as function call stackMostly pure functions
Easier to test & reason aboutTime as first class conceptEasier to distribute
![Page 33: The never changing face of immutability](https://reader031.vdocuments.us/reader031/viewer/2022021923/5a68776b7f8b9a4a258b5015/html5/thumbnails/33.jpg)
RESOURCESRich Hickey talks -
'The Value of Values''The Language of the System''Simple Made Easy''Clojure, Made Simple''The Database as a Value''The Language of Systems'
Moseley and Marks - Out of the Tar PitKris Jenkins
'ClojureScript - Architecting for Scale' (Clojure eXchange 2015)