View
210
Download
0
Category
Tags:
Preview:
DESCRIPTION
A quick talk given at our workplace lunch learning sessions on the different ways to destructure composite data structures in Clojure.
Citation preview
Destructuring Demystified
Destructuring Demystified
A Tour of clojure’s Destructuring forms by someone hopelessly unqualified.
A Tour of clojure’s Destructuring forms by someone hopelessly unqualified.
TIL Session #1TIL Session #1
What is it?
Why use it?
Where should I use them?
When should I use them?
How?
Cool tricks
What?What?“A method of positionally binding locals
based on an expected form for a composite data structure”
- The Joy of ClojureOR
“Picking out the bits you need”- Tom Crinson
Why?Why?
A lot of times you don’t need the whole structure.
Keeps code readable
Keeps functions focussed
Why?Why?
Where?Where?
let form
function parameters
When?When?
“Whenever you find yourself calling nth repeatedly, or looking up constants in a map
or using first or next, ask yourself if restructuring would be a better fit.”
- The Joy of Clojure
Vectors - RemainderVectors - Remainder
Slurp up remaining names
Vectors - :asVectors - :as
Keeps as a vector - untouched
Vectors - Associative Destructuring
Vectors - Associative Destructuring
Declare local names as indices into a vector
MapsMaps
‘Tis a bit repetitive...
And... keywords on the right?
Maps - keywordsMaps - keywords:keys
:strs
:syms ...
Maps - default Values
Maps - default Values
Cool Tricks -1Cool Tricks -1Nested Destructuring
Cool Tricks - 2Cool Tricks - 2
Works on other data structures!
FinFin
Recommended