diff options
| author | Simon Proctor <simon.proctor@zpg.co.uk> | 2019-09-26 11:29:52 +0100 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zpg.co.uk> | 2019-09-26 11:29:52 +0100 |
| commit | 8825c45c8da52c944d063353438e988d355b4d61 (patch) | |
| tree | 5326ebb5d1599a6d29d8e0a17c4c69fb99fcbe3b | |
| parent | 6ed5abfaa1a26717410601a84d89a57d8c17738c (diff) | |
| download | perlweeklychallenge-club-8825c45c8da52c944d063353438e988d355b4d61.tar.gz perlweeklychallenge-club-8825c45c8da52c944d063353438e988d355b4d61.tar.bz2 perlweeklychallenge-club-8825c45c8da52c944d063353438e988d355b4d61.zip | |
Doesn't *quite* hit the spec but does give you objects you can easily use and keep track of the history off
| -rw-r--r-- | challenge-027/simon-proctor/perl6/ch-2.p6 | 88 |
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; |
