Last active
October 2, 2016 21:45
-
-
Save aravindbaskaran/dea834a7332034e1c3250a023017ca67 to your computer and use it in GitHub Desktop.
Destructure examples in JS and Clojure. Related post - https://medium.com/@aravindbaskaran/destructuring-clojure-with-javascript-bd1398bdacb6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Fire up a REPL and try it out | |
(let [list [1 2 3] | |
[x y z] list] | |
(println x y z)) | |
;; Better one | |
(let [[x y z a :as list] [1 2 3]] | |
(println x y z a list)) | |
;; Spread operator | |
(let [[x y z & rest-of-it :as list] [1 2 3 5 6 7 8]] | |
(println x y z rest-of-it)) | |
;; Ignore | |
(let [[x y _ z :as list] [1 2 "ignore-me" 3]] | |
(println x y z)) | |
;; Default values | |
(let [[x,y,z :or [z 10]] [1 2]] | |
(println x y z)) | |
;; Object - not so efficient when there is no renaming | |
(let [{x :x y :y} {:x 1 :y 2 :z 3}] | |
(println x y)) | |
;; Object with renaming | |
(let [{rx :x y :y} {:x 1 :y 2 :z 3}] | |
(println rx y)) | |
;; Object with keywords and strings, but no renaming | |
(let [{:keys [x y z] :as list} {:x 1 :y 2 :z 3} | |
{:strs [xx yy zz] :as list-str} {"xx" 11 "yy" 22 "zz" 33}] | |
(println x y z xx yy zz)) | |
;; Object with defaults | |
(let [{:keys [x y z xx yy zz] :or {xx 10 yy 20 zz 30} :as list} {:x 1 :y 2 :z 3}] | |
(println x y z xx yy zz)) | |
;; Object defaults with renaming | |
(let [{rx :x y :y rz :z :or {rz 30}} {:x 1 :y 2}] | |
(println rx y rz)) | |
;; Usage in functions - not so efficient | |
(defn do-something-awesome [{ | |
mandatoryParam :mandatoryParam | |
changedParam :renameParam | |
optionalParam :optionalParam | |
changedOptionalParam :optionalRenameParam | |
:or {optionalParam 1 changedOptionalParam 2} }] | |
(println mandatoryParam changedParam optionalParam changedOptionalParam)) | |
(do-something-awesome {:mandatoryParam 10 :renameParam 20}) | |
;; Usage in functions - better | |
(defn do-something-awesome [{ | |
:keys [mandatoryParam optionalParam optionalRenameParam] | |
changedParam :renameParam | |
changedOptionalParam :optionalRenameParam | |
:or {optionalParam 1 changedOptionalParam 2} | |
}] | |
(println mandatoryParam changedParam optionalParam changedOptionalParam)) | |
(do-something-awesome {:mandatoryParam 10 :renameParam 20}) | |
;; Nested objects | |
(def o { | |
:a { | |
:x 1 | |
:y [2, 3] | |
} | |
}) | |
;; With default values | |
(let [{ {:keys [x y]} :a} o | |
[y1,y2] y] ;; forced to add a line to break y to params | |
(println x y y1 y2)) | |
;; y1, y2 don't get assigned, what’s wrong? a TODO for me to figure out | |
(let [{ {:keys [x [[y1,y2] :y]]} :a} o] | |
(println x y y1 y2)) | |
;; Not trying the overkill until the above is figured out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Paste away in a browser console or your nodejs console | |
// Old way | |
var list = [1,2]; | |
var x = list[0], y = list[1]; | |
console.log(x, y); | |
// Bunching rest of the values with a spread operator | |
var list = [1,2, 3, 5, 6, 7, 8]; | |
var [x, y, z, …a] = list; | |
console.log(x, y, z, a); | |
// Note - The last param will be the spread operator(...) Powerful for array merging, and data processing. More on spread operators | |
// Ignoring parts | |
var list = [1, 2, "ignore me", 3]; | |
var [x,y,,z] = list; | |
console.log(x,y,z); | |
// Default values | |
var list = [1, 2]; | |
var [x,y,z="default z"] = list; | |
console.log(x,y,z); | |
// Object | |
var o = {"x": 1, "y": 2, "z": 3}; | |
var {x, y, z} = o; | |
console.log(x, y, z); | |
// Also var {x, y, z} = {x: 1, y: 2, z: 3}; | |
// Also var x,y,z; ({x, y, z} = o); | |
// Renaming | |
var o = {"x": 1, "y": 2, "z": 3}; | |
var {x: a, y: b, z: c} = o; | |
console.log(a, b, c); | |
// Default values with renaming | |
var o = {"x": 1, "y": 2, "z": 3}; | |
var {x, y, z: c, p: d=10, q=20} = o; | |
console.log(a, b, c, d, q); | |
// Usage in functions | |
function doSomethingAwesome({mandatoryParam, renameParam: changedParam, optionalParam=1, optionalRenameParam: changedOptionalParam=2}){ | |
console.log(mandatoryParam, changedParam, optionalParam, changedOptionalParam); | |
} | |
doSomethingAwesome({mandatoryParam: 10, renameParam: 20}); | |
// Nested objects | |
var o = { | |
"a": { | |
"x": 1, | |
"y": [2, 3] | |
} | |
}; | |
// With renaming and default values | |
var {a: {x, y: [aa, bb, cc=20], z=10}} = o; | |
console.log(a, x, y, p, aa, bb, cc, z); | |
// Overkill? | |
var o = { | |
"a": { | |
"x": 1, | |
"y": [2, 3] | |
} | |
}; | |
var {a: {x, y: p = [aa, bb, dd=20], z=10}} = o; | |
console.log(a, x, y, p, aa, bb, dd, z); // Throws error, renaming with internal destructuring doesn't work |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment