3

Destructuring in Clojure - Part 1

 3 years ago
source link: https://blog.klipse.tech/clojure/2016/03/30/destructuring.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Destructuring in Clojure - Part 1

Mar 30, 2016 • Yehonathan Sharvit

Introduction

Basically, destructuring allows you to create local bindings in a very succint (and elegant) syntax.

In this article, we will demonstrate the basics of destructuring in clojure, using KLIPSE.

Destructuring a vector

The simplest example is destructuring the first n values of a vector:

xxxxxxxxxx
(def point [5 7])
(let [[x y] point]
  {:x x
   :y y})
the evaluation will appear here (soon)...

A more advanced example is splitting a vector into a head and a tail:

xxxxxxxxxx
(def indexes [1 2 3])
(let [[x & more] indexes]
  {:x x :more more})
xxxxxxxxxx
the evaluation will appear here (soon)...

It’s also worth noticing that you can bind the entire vector to a local using the :as directive.

xxxxxxxxxx
(def indexes [1 2 3])
(let [[x & more :as full-list] indexes]
  {:x x :more more :full-list full-list})
xxxxxxxxxx
the evaluation will appear here (soon)...

Destructuring a map

Simple destructuring on a map is as easy as choosing a local name and providing the key.

xxxxxxxxxx
(def point {:x 5 :y 7})
(let [{the-x :x the-y :y} point]
  {:x the-x :y the-y})
xxxxxxxxxx
the evaluation will appear here (soon)...

As the example shows, the values of :x and :y are bound to locals with the names the-x and the-y.

Usually, you want to create locals with the same name as the keys of the map.

In this case, the syntax becomes even simpler, using the :keys directive:

xxxxxxxxxx
(def point {:x 5 :y 7})
(let [{:keys [x y]} point]
  (+ x y))
xxxxxxxxxx
the evaluation will appear here (soon)...

As with vectors, you can bind the entire map to a local using the :as directive.

Here is how to combine :keys and :as.

xxxxxxxxxx
(def point {:x 5 :y 7})
(let [{:keys [x y]} point]
  (+ x y))
xxxxxxxxxx
the evaluation will appear here (soon)...

And here is how to combine destructuring of vectors and maps (thank you Paul Salaberria):

xxxxxxxxxx
(let [[[one :as vec-one]
       [{:keys [k] :as themap} :as vec-two] :as x]
      [[1] [{:k "val"}]]]
  {:x x
   :one one
   :vec-one vec-one
   :k k
   :themap themap
   :vec-two vec-two})
xxxxxxxxxx
the evaluation will appear here (soon)...

In the next article, we will show more advanced usages og destructuring in clojure.

If you enjoy this kind of interactive articles would you consider a (small) donation💸 on Patreon or at least giving a star⭐ for the Klispe repo on Github?

to stay up-to-date with the coolest interactive articles around the world.

Discover more cool interactive articles about javascript, clojure[script], python, ruby, scheme, c++ and even brainfuck!

Give Klipse a Github star to express how much you appreciate Code Interactivity.

Subscribe to the Klipse newsletter:

Feel free to email me [email protected] for getting practical tips and tricks in writing your first interactive blog post.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK