From 17a8f5c086c67baf6f75789527b2a75e9231e84e Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Tue, 24 Mar 2020 11:07:03 +0000 Subject: Rewrite to fully functional --- challenge-053/simon-proctor/raku/ch-2.p6 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/challenge-053/simon-proctor/raku/ch-2.p6 b/challenge-053/simon-proctor/raku/ch-2.p6 index f68bb6f615..17f8554429 100644 --- a/challenge-053/simon-proctor/raku/ch-2.p6 +++ b/challenge-053/simon-proctor/raku/ch-2.p6 @@ -12,15 +12,16 @@ sub MAIN ( UInt $count is copy where 1 <= * <= 5; ) { my @strings = (''); - while $count > 0 { - $count--; - my @next = (); - for @strings -> $string { - @next.push( |valid-next( $string ) ); - } - @strings = @next; - } - .say for @strings; + + .say for process( @strings, $count ); +} + +multi sub process( @list, 0 ) { + @list; +} + +multi sub process( @list, $count ) { + process( @list.map( { valid-next( $_ ) } ).flat, $count - 1 ); } multi sub valid-next( '' ) { -- cgit From 0e728b197c14f596344dc0557d6ab992133781df Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Tue, 24 Mar 2020 11:46:54 +0000 Subject: Really functional now --- challenge-053/simon-proctor/raku/ch-2.p6 | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/challenge-053/simon-proctor/raku/ch-2.p6 b/challenge-053/simon-proctor/raku/ch-2.p6 index 17f8554429..3294fb8c66 100644 --- a/challenge-053/simon-proctor/raku/ch-2.p6 +++ b/challenge-053/simon-proctor/raku/ch-2.p6 @@ -16,34 +16,38 @@ sub MAIN ( .say for process( @strings, $count ); } -multi sub process( @list, 0 ) { +multi sub process( @list, 0 ) is pure { @list; } -multi sub process( @list, $count ) { +multi sub process( @list, $count ) is pure { process( @list.map( { valid-next( $_ ) } ).flat, $count - 1 ); } -multi sub valid-next( '' ) { +multi sub valid-next( '' ) is pure { ; } -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 * ~~ /'e'$/ ) { - ("{$x}i"); +multi sub valid-next( Str $x where * ~~ /'a'$/ ) is pure { + append-val( $x, ); } -multi sub valid-next( Str $x where * ~~ /'i'$/ ) { - ("{$x}a", "{$x}e", "{$x}o", "{$x}u"); +multi sub valid-next( Str $x where * ~~ /'e'$/ ) is pure { + append-val( $x, ); } -multi sub valid-next( Str $x where * ~~ /'o'$/ ) { - ("{$x}a", "{$x}u"); +multi sub valid-next( Str $x where * ~~ /'i'$/ ) is pure { + append-val( $x, ); } -multi sub valid-next( Str $x where * ~~ /'u'$/ ) { - ("{$x}o", "{$x}e"); +multi sub valid-next( Str $x where * ~~ /'o'$/ ) is pure { + append-val( $x, ); +} + +multi sub valid-next( Str $x where * ~~ /'u'$/ ) is pure { + append-val( $x, ); } -- cgit From 74638dcad26c2a9f8e9004cfea4742f5cc80c27e Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Tue, 24 Mar 2020 11:53:17 +0000 Subject: Further fixes. --- challenge-053/simon-proctor/raku/ch-2.p6 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/challenge-053/simon-proctor/raku/ch-2.p6 b/challenge-053/simon-proctor/raku/ch-2.p6 index 3294fb8c66..5b00e77f03 100644 --- a/challenge-053/simon-proctor/raku/ch-2.p6 +++ b/challenge-053/simon-proctor/raku/ch-2.p6 @@ -9,7 +9,7 @@ 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 = (''); @@ -32,22 +32,22 @@ sub append-val( $val, *@rest ) is pure { @rest.map( { $val ~ $_ } ); } -multi sub valid-next( Str $x where * ~~ /'a'$/ ) is pure { +multi sub valid-next( Str $x where *.ends-with('a') ) is pure { append-val( $x, ); } -multi sub valid-next( Str $x where * ~~ /'e'$/ ) is pure { +multi sub valid-next( Str $x where *.ends-with('e') ) is pure { append-val( $x, ); } -multi sub valid-next( Str $x where * ~~ /'i'$/ ) is pure { +multi sub valid-next( Str $x where *.ends-with('i') ) is pure { append-val( $x, ); } -multi sub valid-next( Str $x where * ~~ /'o'$/ ) is pure { +multi sub valid-next( Str $x where *.ends-with('o') ) is pure { append-val( $x, ); } -multi sub valid-next( Str $x where * ~~ /'u'$/ ) is pure { +multi sub valid-next( Str $x where *.ends-with('u') ) is pure { append-val( $x, ); } -- cgit