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
</code></pre><p>In practice, keywords are very commonly used for map keys so destructuring with <code>{:keys [...]}</code> is very common
200
-
as well.</p><p>Map destructuring also lets us specify default values for keys that may be missing:</p><pre><codeclass="clojure">(defn currency-of
200
+
as well.</p><p>If you want to destructure a map that has namespaced keys, you can either
201
+
specify the prefix on each name in the binding or as a prefix on <code>:keys</code> itself:</p><pre><codeclass="clojure">;; instead of {:currency "GBP" :amount 95.99} let's assume we have namespaced
</code></pre><p>Map destructuring also lets us specify default values for keys that may be missing:</p><pre><codeclass="clojure">(defn currency-of
201
213
[{:keys [currency amount] :or {currency :gbp}}]
202
214
currency)
203
215
</code></pre><p>This is very commonly used for implementing functions that take "extra options" (faking named arguments support).</p><p>Just like with positional destructuring, map destructuring works exactly the same way for let bindings:</p><pre><codeclass="klipse-clojure nohighlight">(let [money {:currency :gbp :amount 10}
Copy file name to clipboardExpand all lines: articles/tutorials/getting_started/index.html
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -98,7 +98,7 @@ <h2>Getting Started</h2>
98
98
repositories) without needing JAR files to be built and deployed.</p><p>Since the release of the official Clojure CLI, usage of Leiningen has dropped
99
99
as the community adopted the new CLI and associated libraries such as
100
100
<code>tools.deps</code> and <code>tools.build</code>.
101
-
<em>[See <ahref="https://clojure.org/news/2022/06/02/state-of-clojure-2022">State of Clojure 2022 Survey Results</a>]</em></p><p><strong>Too Much Information! What Should I Use?</strong></p><p>As noted above, if you're following a book or tutorial that uses Leiningen,
101
+
<em>[See <ahref="https://clojure.org/news/2022/06/02/state-of-clojure-2022">State of Clojure 2022 Survey Results</a> -- and a year later the CLI usage exceeded 70% while Leiningen had dropped to around 60%]</em></p><p><strong>Too Much Information! What Should I Use?</strong></p><p>As noted above, if you're following a book or tutorial that uses Leiningen,
102
102
or working with a project that uses Leiningen, then keep using Leiningen.</p><p>If you're following a book or tutorial that uses the newer CLI, or working
103
103
with a project that uses it, then keep using the Clojure CLI and associated
104
104
libraries.</p><p>If you're starting from scratch, learning the Clojure CLI is probably a
Copy file name to clipboardExpand all lines: articles/tutorials/introduction/index.html
+7-3Lines changed: 7 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -148,11 +148,13 @@ <h2>Introduction to Clojure</h2>
148
148
you want to include a double-quote mark in a string, backslash-escape
149
149
it.</p><pre><codeclass="clojure">#"^foo\d?$" ; A regular expression.
150
150
:foo ; A keyword.
151
+
:foo/bar ; A namespaced keyword.
151
152
</code></pre><p>We'll have more to say about <ahref="#regular-expressions">regular
152
153
expressions</a> later on.</p><p>Keywords are just scalars that evaluate to themselves and are useful
153
154
where in other languages you might use little strings as identifiers
154
155
(for example, as the keys in a hashmap). More about keywords in the
155
-
next section (<ahref="#data-structures">Data Structures</a>).</p><pre><codeclass="clojure">'foo ; A symbol.
156
+
next section (<ahref="#data-structures">Data Structures</a>).</p><pre><codeclass="clojure">'foo ; A symbol.
157
+
'foo/bar ; A namespaced symbol.
156
158
</code></pre><p>A <em>symbol</em> is an object that represents the <em>name</em> of something. The
157
159
single quote mark is there to keep Clojure from trying to figure out
158
160
to what the symbol refers (the quote isn't part of the identifier of
@@ -172,8 +174,10 @@ <h2>Introduction to Clojure</h2>
172
174
[1 :two "three"] ; Put anything into them you like.
173
175
{:a 1 :b 2} ; A hashmap (or just "map", for short).
174
176
</code></pre><p>A hashmap is your typical hash/dictionary data structure. In the above
175
-
example, the keys are :a and :b, and the values are 1 and 2. One key-value
176
-
pair in a map is called an <em>entry</em>.</p><p>Although it's most common to use keywords (as shown above) for hashmap
177
+
example, the keys are <code>:a</code> and <code>:b</code>, and the values are 1 and 2. One key-value
178
+
pair in a map is called an <em>entry</em>.</p><p>Using namespaced keywords as keys is a common practice in Clojure applications,
179
+
as it provides more context to the key and helps avoid key collisions:</p><pre><codeclass="clojure">{:customer/id 123 :invoice/id "INV-456" :invoice/total 95.99}
180
+
</code></pre><blockquote><p>Note: if you have a hashmap where all the keys have the same namespace prefix, such as <code>{:invoice/id "INV-456" :invoice/total 95.99}</code>, the REPL will print it using a shorthand syntax, where the prefix is shown in front of the hashmap and the keys are shown without the prefix: <code>#:invoice{:id "INV-456", :total 95.99}</code>. This is just a shorthand syntax for printing the hashmap and doesn't affect how you use the hashmap in your code.</p></blockquote><p>Although it's most common to use keywords (as shown above) for hashmap
177
181
keys, you can use any values you like for the keys as well as the
178
182
values.</p><pre><codeclass="clojure">#{:a :b :c} ; A set (unordered, and contains no duplicates).
0 commit comments