Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl6
- use v6;
- my $n = 30;
- proto deepclone($) {*}
- multi deepclone(%hash is copy) {
- say "hash";
- %hash{$n++} = 1;
- # there are problems with hyper smartmatching right now
- #%hash = %hash.map( { $_.key => ( ( [||] $_.value <<~~<< ( Positional, Associative ) ) ?? deepclone( $_.value ) !! $_.value ) } );
- my %hash2 = %hash.map( { $_.key => ( ( $_.value ~~ Positional || $_.value ~~ Associative ) ?? deepclone( $_.value ) !! $_.value ) } );
- say "/hash";
- %hash2;
- }
- multi deepclone(@array is copy) {
- say "array";
- @array.push( $n++ );
- # there are problems with hyper smartmatching right now
- #@array = @array.map( { deepclone( $_ ) if [||] $_ <<~~<< ( Positional, Associative ) } );
- my @array2 = @array.map( { ( $_ ~~ Positional || $_ ~~ Associative ) ?? deepclone( $_ ) !! $_ } );
- say "/array";
- @array2;
- }
- multi deepclone($unknown is copy) {
- say "unknown";
- $unknown;
- }
- my $h = { a => 1, b => [ 2, 3, { c => 4, d => 5 } ] };
- my $i = deepclone( $h );
- say $h;
- say $i;
- $i{'b'}[2]{'e'} = 6;
- say "mark a";
- $i{'b'}.unshift( 0 );
- say "mark b";
- $i{'e'} = 7;
- say $h;
- say $i;
- =begin output
- hash
- array
- hash
- /hash
- /array
- /hash
- "a" => 1, "b" => [2, 3, {"c" => 4, "d" => 5}]
- "30" => 1, "a" => 1, "b" => [2, 3, "c" => 4, "d" => 5, "32" => 1, 31]
- Cannot modify an immutable Mu
- in method assign_key at src/gen/m-CORE.setting:1968
- in sub postcircumfix:<{ }> at src/gen/m-CORE.setting:2902
- in block <unit> at t1.p6:38
- =end output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement