3 ms·
If you're like me (and many others) and can't get enough of destructuring, Clojure has it on steroids compared to JS and is everywhere in a normal/average Cloju
by capableweb 4y ago
If you're like me (and many others) and can't get enough of destructuring, Clojure has it on steroids compared to JS and is everywhere in a normal/average Clojure codebase: https://clojure.org/guides/destructuring https://clojure.org/guides/destructuring
Destructuring in Clojure doesn't suffer from the same problems as JavaScript, so lot of the things in the article doesn't apply. I was always slightly disappointed when destructuring finally came to JavaScript but it was so neutered compared to what it could have been.
But on the other hand, I'm happy JavaScript is not becoming more complicated just for the sake of new features. But on the other foot, we already got `class` and a bunch of other complicated, syntactic sugar, so why not another?
- __ryan__ 4y agoI’m not super familiar with Clojure, but I tried to read through the link you shared. I certainly could be missing something, what can Clojure’s destructuring do that JavaScript cannot?
- deleted 4y ago[deleted]
- joshlemer 4y agoI'm not sure what are all the differences but one I have come across that you can't do in JS is to simultaneously destructure an object/array, and at the same time bind a name to the entire object/array. For instance here in Clojure we can extract the a,b,c keys from the passed map, and at the same time bind the whole map to the variable bar. (defn foo [{:keys [a b c] :as bar}] [bar a b c]) (foo {:a 1 :b 2 :c 3}) ;; => [{:a 1, :b 2, :c 3} 1 2 3] in JS you can do const f = ({a,b,c}) => [???, a, b, c] but how do you bind to the whole object as well?
- joshlemer 4y agoOh I thought of an other one. In Clojure "objects" are actually maps, which can have non-string keys as well. That means you can destructure maps which have non-string (or, technically, keyword) keys as well: (defn foo [{a 1 b 2 :as bar}] [bar a b]) (foo {1 "hi" 2 "world" 3 "!"}) ;; => [{1 "hi", 2 "world", 3 "!"} "hi" "world"] In JS of course you can have Maps with non-string keys, but destructuring doesn't work on them. Plus you have other issues like arrays in JS having equality defined by reference rather than value, making maps less useful overall.
- __ryan__ 4y agoAh, I see. Yeah, you would have to bind the argument to a name then destructure it separately: let foo = (bar) => { let {a, b, c} = bar return [bar, a, b, c] } foo({ a: 1, b: 2, c: 3 }) // => [ { a: 1, b: 2, c: 3 }, 1, 2, 3 ] Though JavaScript object keys are strictly strings and symbols, you can destructure dynamic keys like so: let foo = (bar) => { let {[1]: a, [2]: b} = bar return [bar, a, b] } foo({ 1: "hi", 2: "world", 3: "!" }) // => [ { '1': 'hi', , '2': 'world', '3': '!' }, 'hi' 'world' ]
- boxed 4y agoI wish clojure had a syntax for complete destructure. As it is clojure code pretends it has keyword/named arguments but if you change the name of an argument it will pass silently into the night.