Deepcopy: Difference between revisions

Content added Content deleted
(Add Factor)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 905:
print(uneval(src));
</lang>
 
=={{header|jq}}==
The distinction between "deep" and "shallow" copying is as irrelevant in a jq program as in elementary arithmetic. There is only one "equality" operator in jq and it is defined in terms of equality of values.
Line 1,417 ⟶ 1,418:
In PARI, this is accomplished with the command <code>gcopy</code> rather than <code>shallowcopy</code> or <code>leafcopy</code>. The function takes and returns a <code>GEN</code>. See section 10.6 of the [http://pari.math.u-bordeaux.fr/pub/pari/manuals/2.5.0/libpari.pdf User's Guide to the PARI Library].
 
=={{Headerheader|Perl}}==
 
use [http://search.cpan.org/perldoc?Storable Storable]; <code>Storable::dclone()</code> is exactly what you are looking for.
Line 1,434 ⟶ 1,435:
print Dumper($dst);
</lang>
 
=={{header|Perl 6}}==
 
Perl 6 doesn't currently provide a proper mechanism for deep copies, but depending on your requirements you could use one of these work-arounds:
 
<br>
'''1) Use <code>.deepmap(*.clone)</code>:'''
 
<tt>.deepmap</tt> constructs a copy of the data structure, and <tt>.clone</tt> makes a shallow copy of each leaf node. Limitations:
* Hangs indefinitely when given a self-referential data structure.
* Descends only into <tt>Iterable</tt> collections (like <tt>Array</tt>/<tt>Hash</tt>), which means that a <tt>Pair</tt> or a typical custom object would be considered a leaf node.
 
<lang perl6>my %x = foo => 0, bar => [0, 1];
my %y = %x.deepmap(*.clone);
 
%x<bar>[1]++;
say %x;
say %y;</lang>
 
{{out}}
<pre>
{bar => [0 2], foo => 0}
{bar => [0 1], foo => 0}
</pre>
 
<br>
'''2) Use <code>.perl.EVAL</code>:'''
 
<tt>.perl</tt> serializes the data structure to Perl 6 code, and <tt>.EVAL</tt> deserializes it. Limitations:
* Doesn't work correctly if the data structure contains elements that can't be properly serialized, such as closures or file handles.
 
<lang perl6>use MONKEY-SEE-NO-EVAL;
 
my %x = foo => 0, bar => [0, 1];
my %y = %x.perl.EVAL;
 
%x<bar>[1]++;
say %x;
say %y;</lang>
 
{{out}}
<pre>
{bar => [0 2], foo => 0}
{bar => [0 1], foo => 0}
</pre>
 
=={{header|Phix}}==
Line 1,521 ⟶ 1,477:
</pre>
 
=={{Headerheader|PHP}}==
 
PHP provides the <code>clone</code> operator ([http://www.php.net/manual/en/language.oop5.cloning.php docs]) for shallow copying, and allows you to hook into a magic class method called <code>__clone()</code> in your classes to do some of the lifting to create deeper copies, but this method won't create a true deep copy if you don't write the code to manage it in each of the child classes.
Line 1,661 ⟶ 1,617:
deepcopy: (#0=(1 . #0#) #0#)
both: ((#0=(1 . #0#) #0#) (#1=(1 . #1#) #1#))
</pre>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
 
Perl 6 doesn't currently provide a proper mechanism for deep copies, but depending on your requirements you could use one of these work-arounds:
 
<br>
'''1) Use <code>.deepmap(*.clone)</code>:'''
 
<tt>.deepmap</tt> constructs a copy of the data structure, and <tt>.clone</tt> makes a shallow copy of each leaf node. Limitations:
* Hangs indefinitely when given a self-referential data structure.
* Descends only into <tt>Iterable</tt> collections (like <tt>Array</tt>/<tt>Hash</tt>), which means that a <tt>Pair</tt> or a typical custom object would be considered a leaf node.
 
<lang perl6>my %x = foo => 0, bar => [0, 1];
my %y = %x.deepmap(*.clone);
 
%x<bar>[1]++;
say %x;
say %y;</lang>
 
{{out}}
<pre>
{bar => [0 2], foo => 0}
{bar => [0 1], foo => 0}
</pre>
 
<br>
'''2) Use <code>.perl.EVAL</code>:'''
 
<tt>.perl</tt> serializes the data structure to Perl 6 code, and <tt>.EVAL</tt> deserializes it. Limitations:
* Doesn't work correctly if the data structure contains elements that can't be properly serialized, such as closures or file handles.
 
<lang perl6>use MONKEY-SEE-NO-EVAL;
 
my %x = foo => 0, bar => [0, 1];
my %y = %x.perl.EVAL;
 
%x<bar>[1]++;
say %x;
say %y;</lang>
 
{{out}}
<pre>
{bar => [0 2], foo => 0}
{bar => [0 1], foo => 0}
</pre>