prelude-ls
prelude-ls copied to clipboard
Add deep copy function
create a deep copy of objects/arrays
gkz/LiveScript#224
How would this go? Do you need a deep-copy
in both List
and Obj
, or should there one in index
. Reason being, you could have a list with an object, or an object with a list. Both of which should be deep-copy
-ed. So it seems like you'd have the exact same function in both, something like:
deep-copy = ->
match typeof! it
| (is \Object) => {[k, deep-copy v] for k, v of it}
| (is \Array) => [deep-copy i for i in it]
| otherwise => it
Are there other types I'm forgetting?
@joneshf Cycles kill it:
a = {}
a.a = a # self-reference
deep-copy a #=> [ infinite loop ]
Well, without tail-call optimisation, it'll overflow the stack instead.
Deep copy functions have been discussed extensively before and found hard or impossible.