Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Created March 12, 2015 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ab5tract/00a98457cd5c0989efd4 to your computer and use it in GitHub Desktop.
Save ab5tract/00a98457cd5c0989efd4 to your computer and use it in GitHub Desktop.
class Codeword {
has Supply $.printer;
# has Sub $!codeword;
has Sub $.scrambler is rw;
has $!string;
has $!encoded-string;
method new( :$string = "bloop" ) {
my $printer = Supply.new;
$printer.act: { $_.say };
self.bless( :$string, :$printer );
}
submethod BUILD( :$!string, :$!printer ) {
# my sub codeword is rw {
# return Proxy.new:
# FETCH => method () { $!encoded-string },
# STORE => method ($s) { $!encoded-string = scrambler($s) };
# }
# $!codeword = &codeword;
my sub scrambler { $^s.trans: 'a..mn..z' => 'n..za..m', :ii }
$!scrambler := &scrambler;
}
sub codeword is rw {
return Proxy.new:
FETCH => method () { $!encoded-string },
STORE => method ($s) { $!encoded-string = $!scrambler($s) };
}
method encode( Str $string ) {
codeword() = $string;
}
method display {
$!printer.emit( $!encoded-string ||= $!scrambler($!string) );
# say $!encoded-string ||= scrambler($s);
if codeword() {
codeword();
} else {
codeword() = $!string;
codeword();
}
}
}
my $code = Codeword.new;
$code.display;
+ proxy-test.p6 (22 - 26)
| # $!codeword = &codeword;
|
| my sub scrambler { $^s.trans: 'a..mn..z' => 'n..za..m', :ii }
| $!scrambler := &scrambler;
| }
>
+ proxy-test.p6 (9 - 13)
| method new( :$string = "bloop" ) {
| my $printer = Supply.new;
| $printer.act: { $_.say };
|
| self.bless( :$string, :$printer );
>
oybbc
+ proxy-test.p6 (39 - 43)
| $!printer.emit( $!encoded-string ||= $!scrambler($!string) );
| # say $!encoded-string ||= scrambler($s);
| if codeword() {
| codeword();
| } else {
>
+ proxy-test.p6 (27 - 33)
|
| sub codeword is rw {
| return Proxy.new:
| FETCH => method () { $!encoded-string },
| STORE => method ($s) { $!encoded-string = $!scrambler($...
| }
|
>
+ proxy-test.p6 (28 - 32)
| sub codeword is rw {
| return Proxy.new:
| FETCH => method () { $!encoded-string },
| STORE => method ($s) { $!encoded-string = $!scrambler($...
| }
>
~/.rakudobrew/bin/perl6-debug-m: line 2: 6394 Segmentation fault: 11 /Users/jhaltiwanger/.rakudobrew/bin/../moar-HEAD/install/bin/perl6-debug-m "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment