diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-25 18:44:43 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-25 18:44:43 +0000 |
| commit | 80e58afb2cbc5cfcd8fa81082c7774cc3dad040a (patch) | |
| tree | 68129d9249966113e900d5a9aa40f734bd1f0e8e | |
| parent | f9ddd281b0676717a06f9457acfb53a69b0edec5 (diff) | |
| parent | 74638dcad26c2a9f8e9004cfea4742f5cc80c27e (diff) | |
| download | perlweeklychallenge-club-80e58afb2cbc5cfcd8fa81082c7774cc3dad040a.tar.gz perlweeklychallenge-club-80e58afb2cbc5cfcd8fa81082c7774cc3dad040a.tar.bz2 perlweeklychallenge-club-80e58afb2cbc5cfcd8fa81082c7774cc3dad040a.zip | |
Merge pull request #1464 from Scimon/master
Rewrite to fully functional
| -rw-r--r-- | challenge-053/simon-proctor/raku/ch-2.p6 | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/challenge-053/simon-proctor/raku/ch-2.p6 b/challenge-053/simon-proctor/raku/ch-2.p6 index f68bb6f615..5b00e77f03 100644 --- a/challenge-053/simon-proctor/raku/ch-2.p6 +++ b/challenge-053/simon-proctor/raku/ch-2.p6 @@ -9,40 +9,45 @@ use v6; #| ‘o’ can only be followed by ‘a’ and ‘u’. #| ‘u’ can only be followed by ‘o’ and ‘e’. sub MAIN ( - UInt $count is copy where 1 <= * <= 5; + UInt $count where 1 <= * <= 5; ) { my @strings = (''); - while $count > 0 { - $count--; - my @next = (); - for @strings -> $string { - @next.push( |valid-next( $string ) ); - } - @strings = @next; - } - .say for @strings; -} - -multi sub valid-next( '' ) { + + .say for process( @strings, $count ); +} + +multi sub process( @list, 0 ) is pure { + @list; +} + +multi sub process( @list, $count ) is pure { + process( @list.map( { valid-next( $_ ) } ).flat, $count - 1 ); +} + +multi sub valid-next( '' ) is pure { <a e i o u>; } -multi sub valid-next( Str $x where * ~~ /'a'$/ ) { - ("{$x}e", "{$x}i"); +sub append-val( $val, *@rest ) is pure { + @rest.map( { $val ~ $_ } ); +} + +multi sub valid-next( Str $x where *.ends-with('a') ) is pure { + append-val( $x, <e i> ); } -multi sub valid-next( Str $x where * ~~ /'e'$/ ) { - ("{$x}i"); +multi sub valid-next( Str $x where *.ends-with('e') ) is pure { + append-val( $x, <i> ); } -multi sub valid-next( Str $x where * ~~ /'i'$/ ) { - ("{$x}a", "{$x}e", "{$x}o", "{$x}u"); +multi sub valid-next( Str $x where *.ends-with('i') ) is pure { + append-val( $x, <a e o u> ); } -multi sub valid-next( Str $x where * ~~ /'o'$/ ) { - ("{$x}a", "{$x}u"); +multi sub valid-next( Str $x where *.ends-with('o') ) is pure { + append-val( $x, <a u> ); } -multi sub valid-next( Str $x where * ~~ /'u'$/ ) { - ("{$x}o", "{$x}e"); +multi sub valid-next( Str $x where *.ends-with('u') ) is pure { + append-val( $x, <o e> ); } |
