RosettaCodeData/Task/Deepcopy/Deja-Vu/deepcopy.djv
2014-01-17 05:34:36 +00:00

52 lines
820 B
Text

local :(copy-action) {}
(copy-action)!list obj cache:
local :new []
set-to cache obj new
for i range 0 -- len obj:
push-to new (deepcopy) @obj! i cache
return new
(copy-action)!dict obj cache:
local :new {}
set-to cache obj new
for key in keys obj:
set-to new (deepcopy) @key cache (deepcopy) @obj! @key cache
return new
labda obj cache:
set-to cache @obj dup copy @obj
set-default (copy-action)
(deepcopy) obj cache:
if has cache obj:
return @cache! @obj
(copy-action)! type @obj @obj cache
deepcopy obj:
(deepcopy) obj {}
#example usage:
#a reasonably complicated object:
set :A { :foo [ "bar" ] [] [ & 1 2 & 3 4 ] }
set :B deepcopy A
!. A
!. B
push-to get-from B :foo "HODOR"
!. A
!. B
#it works with cycles:
set :C push-through dup []
set :D deepcopy C
!. C
!. D
push-to C 7
!. C
!. D