Created
March 18, 2018 02:08
-
-
Save lwhorton/8a95a18d8d741f87c0c3ab214c67111c to your computer and use it in GitHub Desktop.
why-clojure#3.clj
This file contains hidden or 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
| (let [old {:a 1 :b 2} | |
| new-o (assoc old :c 3)] | |
| (prn old) ;; {:a 1 :b 2} | |
| ) | |
| (let [arr [1 2 3] | |
| new-a (conj arr 4)] | |
| (prn arr) ;; [1 2 3] | |
| ) | |
| (let [obj-arr [{:a 1} {:b 2} {:c 3}] | |
| new-o-a (assoc obj-arr 1 {:b -2})] | |
| (prn obj-arr) ;; {:a 1} {:b 2} {:c 3}] | |
| (prn new-o-a) ;; {:a 1} {:b -2} {:c 3}] | |
| ) |
This file contains hidden or 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
| var old = {a: 1, b: 2} | |
| var newO = Object.assign({}, {...old, c: 3}) | |
| console.log(old) // { a: 1, b: 2 } | |
| var arr = [1, 2, 3] | |
| var newA = [...arr, 4] | |
| console.log(arr) // [1, 2, 3] | |
| // what about updates/insertions? careful not to miss any ellipses or slices or off-by-ones | |
| var objArr = [{a: 1}, {b: 2}, {c: 3}] | |
| var newOA = [...objArr.slice(0, idx), {...objArr[idx], b: -2}, ...objArr.slice(idx+1)] | |
| console.log(objArr) // [{a: 1}, {b: 2}, {c: 3}] | |
| console.log(newOA) // [{a: 1}, {b: -2}, {c: 3}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment