Advertisement
Guest User

The Mysterious Immutable Mu

a guest
Nov 13th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 1.52 KB | None | 0 0
  1. #!/usr/bin/env perl6
  2.  
  3. use v6;
  4.  
  5. my $n = 30;
  6.  
  7. proto deepclone($) {*}
  8. multi deepclone(%hash is copy) {
  9.     say "hash";
  10.     %hash{$n++} = 1;
  11.     # there are problems with hyper smartmatching right now
  12.     #%hash = %hash.map( { $_.key => ( ( [||] $_.value <<~~<< ( Positional, Associative ) ) ?? deepclone( $_.value ) !! $_.value ) } );
  13.     my %hash2 = %hash.map( { $_.key => ( ( $_.value ~~ Positional || $_.value ~~ Associative ) ?? deepclone( $_.value ) !! $_.value ) } );
  14.     say "/hash";
  15.     %hash2;
  16. }
  17. multi deepclone(@array is copy) {
  18.     say "array";
  19.     @array.push( $n++ );
  20.     # there are problems with hyper smartmatching right now
  21.     #@array = @array.map( { deepclone( $_ ) if [||] $_ <<~~<< ( Positional, Associative ) } );
  22.     my @array2 = @array.map( { ( $_ ~~ Positional || $_ ~~ Associative ) ?? deepclone( $_ ) !! $_ } );
  23.     say "/array";
  24.     @array2;
  25. }
  26. multi deepclone($unknown is copy) {
  27.     say "unknown";
  28.     $unknown;
  29. }
  30.  
  31. my $h = { a => 1, b => [ 2, 3, { c => 4, d => 5 } ] };
  32.  
  33. my $i = deepclone( $h );
  34.  
  35. say $h;
  36. say $i;
  37.  
  38. $i{'b'}[2]{'e'} = 6;
  39. say "mark a";
  40. $i{'b'}.unshift( 0 );
  41. say "mark b";
  42. $i{'e'} = 7;
  43.  
  44. say $h;
  45. say $i;
  46.  
  47. =begin output
  48. hash
  49. array
  50. hash
  51. /hash
  52. /array
  53. /hash
  54. "a" => 1, "b" => [2, 3, {"c" => 4, "d" => 5}]
  55. "30" => 1, "a" => 1, "b" => [2, 3, "c" => 4, "d" => 5, "32" => 1, 31]
  56. Cannot modify an immutable Mu
  57.   in method assign_key at src/gen/m-CORE.setting:1968
  58.   in sub postcircumfix:<{ }> at src/gen/m-CORE.setting:2902
  59.   in block <unit> at t1.p6:38
  60.  
  61. =end output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement