prelude-ls icon indicating copy to clipboard operation
prelude-ls copied to clipboard

Add deep copy function

Open gkz opened this issue 12 years ago • 2 comments

create a deep copy of objects/arrays

gkz/LiveScript#224

gkz avatar Dec 30 '12 05:12 gkz

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 avatar Jul 21 '13 18:07 joneshf

@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.

anko avatar Jul 31 '14 14:07 anko