Clojure Destructuring aka "abstract structural binding"

These are my notes for a talk I prepared for a clojure user group meeting.

What is it?

Sometimes said to be clojures way to do named parameters but it's much more than that. It is a way to take apart a structure into multiple substructures typically when assigning variables.

Let's dive in with examples! It works on lists:

and on maps:

It goes deeper into the structure:

and there's the fancy stuff like "keys", "strs" and "syms":

Also "as" and "or":

Can it be used outside of "let"?

Yes, many expressions support it, like "defn" or rather "fn":

And many more, like "loop", "doseq":

Anything in core having some kind of binding, params, seq-exprs supports this.

How does it work?

Let's investigate! What is "let"?

So what happens when we use it?

The destructure function?

Any seq will do?

Really seq?

Aha! Finally! Named parameters!

Instead of the unspliced:

What about the doc string of my new function?

What about all the named parameters?

Only lists and maps?

Hmm, let's look at RT.java:

https://github.com/clojure/clojure/blob/clojure-1.7.0/src/jvm/clojure/lang/RT.java#L719

Anything we can "get" or "nth"; strings!

Use your own types!

Pretty cool, right? Thanks for listening.

--

📅 2014-04-15

📧 hello@rwv.io

CC BY-NC-SA 4.0

Proxied content from gemini://rwv.io/articles/clojure-destructuring.gmi (external content)

Gemini request details:

Original URL
gemini://rwv.io/articles/clojure-destructuring.gmi
Status code
Success
Meta
text/gemini; lang=en
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.