aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-09-26 12:41:39 +0100
committerGitHub <noreply@github.com>2019-09-26 12:41:39 +0100
commit6f8e905f52b85a476df0c79b0268ea6c60311182 (patch)
tree254683dcc02e4c38557d39124b2b98c0b1c3c1ca
parentf1e85e652e1c155f4a79c64f45f985ef002191d2 (diff)
parent8825c45c8da52c944d063353438e988d355b4d61 (diff)
downloadperlweeklychallenge-club-6f8e905f52b85a476df0c79b0268ea6c60311182.tar.gz
perlweeklychallenge-club-6f8e905f52b85a476df0c79b0268ea6c60311182.tar.bz2
perlweeklychallenge-club-6f8e905f52b85a476df0c79b0268ea6c60311182.zip
Merge pull request #670 from Scimon/master
Doesn't *quite* hit the spec but does give you objects you can easily…
-rw-r--r--challenge-027/simon-proctor/perl6/ch-2.p688
1 files changed, 88 insertions, 0 deletions
diff --git a/challenge-027/simon-proctor/perl6/ch-2.p6 b/challenge-027/simon-proctor/perl6/ch-2.p6
new file mode 100644
index 0000000000..f66d74a2c0
--- /dev/null
+++ b/challenge-027/simon-proctor/perl6/ch-2.p6
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+class Historic {
+ has @!values = [];
+
+ method val($main:) is rw {
+ Proxy.new(
+ FETCH => method () { $main.get() },
+ STORE => method ( $new ) { $main.set($new) }
+ );
+ }
+
+ method set( $value ) {
+ @!values.push( $value );
+ $value;
+ }
+
+ method get() {
+ @!values ?? @!values[*-1] !! Nil;
+ }
+
+ method history() {
+ return @!values.list;
+ }
+
+ method from( *@values ) {
+ my $h = Historic.new();
+ $h.set($_) for @values;
+ $h;
+ }
+
+ method perl() {
+ "Historic.from({self.history.join(",")})";
+ }
+
+ method gist() {
+ self.get().gist();
+ }
+
+ method Str() {
+ self.val.Str();
+ }
+
+ method Numeric() {
+ self.val.Numeric();
+ }
+
+}
+
+multi sub infix:<Δ=> ( Any:U $h is rw, Any $v ) is equiv(&infix:<=>) {
+ $h = Historic.new();
+ $h.set( $v );
+ $h;
+}
+
+multi sub infix:<Δ=> (Historic:D $h, Any $a) is equiv(&infix:<=>) {
+ $h.set($a);
+ return $h;
+}
+
+say "Examples";
+my $a = Historic.new();
+$a.set(5);
+$a Δ= 6;
+
+say $a;
+say $a.perl;
+say $a.history;
+
+my $b Δ= 9;
+say $b + 9;
+$b Δ= $b + 9;
+say $b;
+say $b.perl;
+
+my $c = Historic.from(1,2,3,4);
+say $c;
+say $c.history;
+
+my $d = Historic.new();
+$d.val = 5;
+say $d;
+$d.val = 7;
+$d.val += 6;
+say $d;
+say $d.history;