diff options
| author | Markus "Holli" Holzer <holli.holzer@gmail.com> | 2020-04-12 23:59:52 +0200 |
|---|---|---|
| committer | Markus "Holli" Holzer <holli.holzer@gmail.com> | 2020-04-12 23:59:52 +0200 |
| commit | 7c6455cb5e3f133ee10dbc677a3520994ec5fdfc (patch) | |
| tree | aee9e51c7e300dbf84a926c81c0711e6665644bc /challenge-055 | |
| parent | a6800294e5e1f28907698beea29bc8753996abe0 (diff) | |
| download | perlweeklychallenge-club-7c6455cb5e3f133ee10dbc677a3520994ec5fdfc.tar.gz perlweeklychallenge-club-7c6455cb5e3f133ee10dbc677a3520994ec5fdfc.tar.bz2 perlweeklychallenge-club-7c6455cb5e3f133ee10dbc677a3520994ec5fdfc.zip | |
x
Diffstat (limited to 'challenge-055')
| -rw-r--r-- | challenge-055/markus-holzer/raku/ch-1.p6 | 4 | ||||
| -rw-r--r-- | challenge-055/markus-holzer/raku/ch-2.p6 | 17 | ||||
| -rw-r--r-- | challenge-055/markus-holzer/raku/lib/Listlike/Utils.pm6 | 107 |
3 files changed, 118 insertions, 10 deletions
diff --git a/challenge-055/markus-holzer/raku/ch-1.p6 b/challenge-055/markus-holzer/raku/ch-1.p6 index bc201f655d..7886691bb4 100644 --- a/challenge-055/markus-holzer/raku/ch-1.p6 +++ b/challenge-055/markus-holzer/raku/ch-1.p6 @@ -2,7 +2,7 @@ subset BinaryStr of Str where * ~~ /^ <[01]>+ $/; sub USAGE() { print Q:c:to/EOH/; - ch-1.p6 <binary-str> + ch-1.p6 <binary-str> Example: ch-1.p6 010 @@ -20,7 +20,7 @@ sub MAIN( BinaryStr $input ) sub top-flips-of( BinaryStr $input ) { my %result = flips-of( $input ); - |%result{ %result.keys.sort.tail }; + |%result{ %result.keys.max }; } sub flips-of( BinaryStr $input ) diff --git a/challenge-055/markus-holzer/raku/ch-2.p6 b/challenge-055/markus-holzer/raku/ch-2.p6 index a2bdbe07a4..b582c832e3 100644 --- a/challenge-055/markus-holzer/raku/ch-2.p6 +++ b/challenge-055/markus-holzer/raku/ch-2.p6 @@ -1,3 +1,5 @@ +use Listlike::Utils; + sub USAGE() { print Q:c:to/EOH/; ch-2.p6 <n1> <n2> <n3> ... <nn> @@ -11,9 +13,9 @@ sub MAIN( *@input where *.elems > 1 ) { CATCH { USAGE() and exit -1; } - .say for ( @input>>.Int ) - .permutations - .grep: &is-wavy; + ( @input>>.Int ).permutations + .grep( &is-wavy ) + .do( &say ); } sub is-wavy( @n ) returns Bool @@ -22,9 +24,8 @@ sub is-wavy( @n ) returns Bool my @cmp = ( * <= * ), ( * >= * ); - not so @n - .rotor( 2 => -1 ) - .first({ - not @cmp[ $idx = $idx +^ 1 ]( |$_ ); - }); + # I'm using the all - method of my Listlike::Utils module + # for the sake of clarity: # .all(...) vs. not so .first(...) + @n .rotor( 2 => -1 ) + .all({ &@cmp[ $idx = $idx +^ 1 ]( |$_ ) }); }
\ No newline at end of file diff --git a/challenge-055/markus-holzer/raku/lib/Listlike/Utils.pm6 b/challenge-055/markus-holzer/raku/lib/Listlike/Utils.pm6 new file mode 100644 index 0000000000..0da3eaf26c --- /dev/null +++ b/challenge-055/markus-holzer/raku/lib/Listlike/Utils.pm6 @@ -0,0 +1,107 @@ +unit module Listlike::Utils; + +use MONKEY-TYPING; + +subset Code where .WHAT ~~ Callable|WhateverCode; + +multi sub all( $self, Mu $value ) is export +{ + $self.all( * ~~ $value ); +} + +multi sub all( $self, Code $condition ) is export +{ + for |$self { + return False unless .&$condition; + } + + True; +} + +multi sub none( $self, Mu $value ) is export +{ + $self.none( * ~~ $value ); +} + +multi sub none( $self, Code $condition ) is export +{ + for |$self { + return False if .&$condition; + } + + True; +} + +sub do( $self, Code $block ) is export +{ + for |$self { + .&$block; + } +} + +multi sub until( $self, Mu $value, :$ex ) is export +{ + $self.until( * ~~ $value, :$ex); +} + +multi sub until( $self, Code $condition, :$ex ) is export +{ + gather for |$self + { + my $result = .&$condition; + + last if so $ex && $result ; + .take; + last if $result; + } +} + +multi sub hence( $self, Mu $value, :$ex ) is export +{ + $self.hence( * ~~ $value, :$ex ); +} + +multi sub hence( $self, Code $condition, :$ex ) is export +{ + my $take; + + gather for |$self + { + my $result = .&$condition; + + $take = $result + if !$take and !$ex; + + .take and next + if $take; + + $take = $result; + } +} + +augment class List +{ + method none( $condition ) { none( self, $condition ) } + method all( $condition ) { all( self, $condition ) } + method do( $condition ) { do( self, $condition ) } + method until( $condition, :$ex ) { until( self, $condition, :$ex ) } + method hence( $condition, :$ex ) { hence( self, $condition, :$ex ) } +} + +augment class Seq +{ + method none( $condition ) { none( self, $condition ) } + method all( $condition ) { all( self, $condition ) } + method do( $condition ) { do( self, $condition ) } + method until( $condition, :$ex ) { until( self, $condition, :$ex ) } + method hence( $condition, :$ex ) { hence( self, $condition ) } +} + +augment class Range +{ + method none( $condition ) { none( self, $condition ) } + method all( $condition ) { all( self, $condition ) } + method do( $condition ) { do( self, $condition ) } + method until( $condition, :$ex ) { until( self, $condition, :$ex ) } + method hence( $condition, :$ex ) { hence( self, $condition, :$ex ) } +}
\ No newline at end of file |
