4 ms·
First of all, I certainly wasn't claiming that you couldn't look up more than one hash key on one line of code, using a function. My point is that you might re
by dreish 17y ago
First of all, I certainly wasn't claiming that you couldn't look up more than one hash key on one line of code, using a function. My point is that you might reasonably want to extend the concept of destructuring further to cover dictionaries, and that macros allow you to extend the language in that way.
Clarity, readability, and comprehensibility are three words that, in programming, mean pretty much the same thing as familiarity. To me, if I weren't looking at the definition of unpack, the above would look weird because I'd think of the Perl function unpack(), which does something very different. unpack(d, keys) above isn't something you'd even write in Clojure because it would be a synonym for (map d keys). Aside from that, it does seem very straightforward, if limited. Clojure does win on generality, because the Clojure version nests, covering sequential data structures at the same time:
user=> (def d {:a {:a1 [1 2 3] :a2 4} :b 5})
#'user/d
user=> (let [{{[_ _ x] :a1} :a, y :b} d]
[x y])
[3 5]
Hashes are the idiomatic general-purpose object in Clojure, so this sort of capability is actually useful in taking apart function arguments.
- tome 17y agoAh well that nesting in Clojure is very cool! Python can unpack nested tuples, but my "unpack" function would be very unwieldy there.