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
The second way uses +juxt+ and yields the same results for most use cases while also
34
-
being more concise:
26
+
Use +juxt+ when order matters.
35
27
36
28
[source,clojure]
37
29
----
38
-
((juxt :name :culture :weapon) character)
39
-
;; ["Galdalf" "Wizards" "Wizard Staff"]
30
+
;; What are the red and green bean totals?
31
+
((juxt :red :green) beans)
32
+
;; -> [10 1]
40
33
----
41
34
42
-
43
35
==== Discussion
44
36
45
-
Now you might be thinking when to choose one approach over the other.
46
-
47
-
In most cases it won't matter as both approaches return the same _sequence_ of values. The difference lies in the details.
48
-
49
-
As you can see above, the result of getting the list of values by using +vals+ doesn't guarantee any type of ordering. This might or might not be a problem depending on what you intend to do with the values. Also note that it returns a _sequence_, whereas +juxt+ returns a _vector_.
37
+
+juxt+ and the combination of +vals+ and +select-keys+ are both apt
38
+
tools for retrieving multiple keys from a map. There are subtleties to
39
+
their behavior that are important to understand.
50
40
51
-
It's also interesting to note that +select-keys+ receives a sequence of keys.
41
+
At first glance the +juxt+ approach seems to be the clear winner of
42
+
the two. The +juxt+ approach only goes so far; the approach falls
43
+
apart when any of the keys you wish to retrieve is not a keyword (more
44
+
specifically, not a *function*.) This is because +juxt+ merely
45
+
_juxtaposes_ the return values of multiple functions. Since keywords
46
+
are functions, it's possible to +juxt+ them and retrieve a
47
+
strongly-ordered list of values.
52
48
53
-
+juxt+ on the other hand returns the values in the order in which they were provided the function. To see why that is we need to understand how +juxt+ works.
54
-
55
-
Being a variadic function, +juxt+ accepts one or more arguments. These arguments have to be functions - which is very convenient to us since keywords can be used as functions in Clojure.
56
-
57
-
Let these functions be +f1+, +f2+ ... +fN+.
58
-
59
-
The result is itself another variadic function which, when called, returns a vector of the results of applying +f1+, +f2+ ... +fN+ to its arguments, one by one.
60
-
61
-
A few examples might illustrate this better than I can explain:
49
+
If the keys in the +beans+ map were strings, it would not be possible
50
+
to retrieve their values with +juxt+.
62
51
63
52
[source,clojure]
64
53
----
65
-
;
66
-
; Check how vectors and lists respond to various sequence predicates
+juxt+ is one of those functions that you just got to have up your sleeve. You will not use it often, but when the scenario presents itself, you'll be glad you know about it.
0 commit comments