Download - Destructuring demystified

Transcript
Page 1: Destructuring demystified

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.

Page 2: Destructuring demystified

TIL Session #1TIL Session #1

What is it?

Why use it?

Where should I use them?

When should I use them?

How?

Cool tricks

Page 3: Destructuring demystified

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

Page 4: Destructuring demystified

Why?Why?

A lot of times you don’t need the whole structure.

Keeps code readable

Keeps functions focussed

Page 5: Destructuring demystified

Why?Why?

Page 6: Destructuring demystified

Where?Where?

let form

function parameters

Page 7: Destructuring demystified

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

Page 8: Destructuring demystified

Vectors - RemainderVectors - Remainder

Slurp up remaining names

Page 9: Destructuring demystified

Vectors - :asVectors - :as

Keeps as a vector - untouched

Page 10: Destructuring demystified

Vectors - Associative Destructuring

Vectors - Associative Destructuring

Declare local names as indices into a vector

Page 11: Destructuring demystified

MapsMaps

‘Tis a bit repetitive...

And... keywords on the right?

Page 12: Destructuring demystified

Maps - keywordsMaps - keywords:keys

:strs

:syms ...

Page 13: Destructuring demystified

Maps - default Values

Maps - default Values

Page 14: Destructuring demystified

Cool Tricks -1Cool Tricks -1Nested Destructuring

Page 15: Destructuring demystified

Cool Tricks - 2Cool Tricks - 2

Works on other data structures!

Page 16: Destructuring demystified

FinFin