diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-09-29 05:31:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-29 05:31:26 +0100 |
| commit | 84878ddb99174d55837e2691b32d1e71129e9d37 (patch) | |
| tree | f6837be931eb882ec461e2737da395e751ebd613 | |
| parent | 421c4ba8e1ec2f6373802f4165c8e9e80153dfb5 (diff) | |
| parent | 789501fad5adecbb93a7bfb0dfbe1321e7de1248 (diff) | |
| download | perlweeklychallenge-club-84878ddb99174d55837e2691b32d1e71129e9d37.tar.gz perlweeklychallenge-club-84878ddb99174d55837e2691b32d1e71129e9d37.tar.bz2 perlweeklychallenge-club-84878ddb99174d55837e2691b32d1e71129e9d37.zip | |
Merge pull request #677 from jmaslak/master
Joelle's Perl 6 solution to 27.2
| -rwxr-xr-x | challenge-027/joelle-maslak/perl6/ch-2.p6 | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-027/joelle-maslak/perl6/ch-2.p6 b/challenge-027/joelle-maslak/perl6/ch-2.p6 new file mode 100755 index 0000000000..8183d58d79 --- /dev/null +++ b/challenge-027/joelle-maslak/perl6/ch-2.p6 @@ -0,0 +1,59 @@ +#!/usr/bin/env perl6 +use v6; + +# Limitation: With nested data structures, we only store the version as +# it existed at the time of setting the proxy (I.E. an array that +# changes *after* the proxy variable is set will only be stored as the +# initial value of the array. +class History { + has @!hist; + has $!data; + + method get-proxy() is rw { + my $data := $!data; + my $history := @!hist; + Proxy.new( + FETCH => method () { $data }, + STORE => method ($val) { $data = $val; $history.push( $data.clone ) }, + ); + } + + method history() { + my @h = @!hist; + @h.push: $!data; + return @h; + } +} + +sub MAIN() { + my $hist = History.new; + my $x := $hist.get-proxy(); + + $x = 10; + $x = 20; + $x -= 5; + + my $y := $hist.get-proxy(); # It's okay to have multiple proxies + $y++; + $y = 'Foo!'; + + # And to show that the second proxy is using the same values + $x ~= ' Bar!'; + + # A new instance of history should be independnet. + my $hist2 = History.new; + my $z := $hist2.get-proxy(); + $z = 3; # Won't show up in history for $hist. + + # And we just set the original history, the one we log, to a new + # value + $x = 'Baz.'; + + # Also let's do an array. + my @a = 1,2,3; + $x = @a; + + say join("\n", $hist.history».perl); +} + + |
