You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guides/destructuring.adoc
+19-9Lines changed: 19 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -41,9 +41,19 @@ This is perfectly valid, but the code extracting and naming the values in the ve
41
41
;= "Line from ( 5 , 10 ) to ( 10 , 20 )"
42
42
----
43
43
44
-
Rather than explicitly binding each variable, we describe the bindings based on their sequential order. That's a pretty weird statement, "describe the bindings," so let's look at it again.
44
+
Rather than explicitly binding each variable, we describe where the values for the bindings can be found, based on their sequential order.
45
45
46
-
We have a data structure `my-line` that looks like this, `[[5 10] [10 20]]`. In our destructuring form we will create a vector containing two elements, `p1` and `p2`, each of which are vectors themselves. This will bind the vector `[5 10]` to the symbol `p1` and the vector `[10 20]` to the symbol `p2`. Since we want to work with the elements of `p1` and `p2` rather than the structures themselves, we destructure `p1` and `p2` within the same let statement. The vector `p1` looks like this, `[5 10]`, so to destructure it, we create a vector containing two elements, `x1` and `y1`. This binds `5` to the symbol `x1` and `10` to the symbol `y1`. The same is repeated for `p2` binding `10` to `x2` and `20` to `y2`. At this point, we now have everything we need to work with our data.
46
+
We start with a data structure `my-line` that looks like `[[5 10] [10 20]]`. In the destructuring form we describe the structure of the data as a vector containing two elements, `p1` and `p2`, each of which are vectors themselves. This form binds the vector `[5 10]` to the symbol `p1` and the vector `[10 20]` to the symbol `p2`. Since we want to work with the elements of `p1` and `p2` rather than the structures themselves, we destructure `p1` and `p2` within the same let statement. The vector `p1` looks like this, `[5 10]`, so to destructure it, we describe its form as a vector containing two elements, `x1` and `y1`. This binds `5` to the symbol `x1` and `10` to the symbol `y1`. The same is repeated for `p2` binding `10` to `x2` and `20` to `y2`. At this point, we now have everything we need to work with our data.
47
+
48
+
An even more concise version of this would recursively destructure without needing to assign bindings for `p1` or `p2` at all:
49
+
50
+
[source,clojure]
51
+
----
52
+
;= Using the same vector as above
53
+
(let [[[x1 y1] [x2 y2]] my-line]
54
+
(println "Line from (" x1 "," y1 ") to (" x2 ", " y2 ")"))
55
+
;= "Line from ( 5 , 10 ) to ( 10 , 20 )"
56
+
----
47
57
48
58
== Sequential Destructuring
49
59
@@ -77,7 +87,7 @@ This type of destructuring can be used on any kind of data structure that can be
77
87
78
88
The key to sequential destructuring is that you bind the values one-by-one to the symbols in the vector. For instance the vector `[x y z]` will match each element one-by-one with the list `'(1 2 3)`.
79
89
80
-
In some cases, the collection you are destructuring isn't the exact same size as the destructuring bindings. If the vector is too small, the extra symbols will be bound to nil.
90
+
In some cases, the collection you are destructuring isn't the exact same size as the destructuring bindings. If the vector is too small, the extra symbols will be bound to nil. In general, if the binding form does not match a value, the binding will be nil.
81
91
82
92
[source,clojure]
83
93
----
@@ -362,18 +372,18 @@ To support this style of invocation, associative destructuring also works with l
The use of keyword arguments had fallen in and out of fashion in the Clojure community over the years. They are now mostly used when presenting interfaces that people are expected to type at the REPL or the outermost layers of an API. In general, inner layers of the code found it easier to pass options as an explicit map. However, in Clojure 1.11 the capability was added to allow passing of alternating key->values, or a map of those same mappings, or even a map with key->values before it to functions expecting keyword arguments. Therefore, the call to `configure` above can take any of the following forms in addition to those shown above:
0 commit comments