diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-04-09 11:27:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-09 11:27:29 +0100 |
| commit | b4e0df4ed578da57dafe1be557b952aa3afe3dbd (patch) | |
| tree | 25a73e5c7b499d2b23807b66c0ac804ff8cacadb | |
| parent | e31c45a5e1515c66143d4f70bb6e1a2476b48da4 (diff) | |
| parent | 72a6744070c9f4e2ebc1d9425f16d46346cd0739 (diff) | |
| download | perlweeklychallenge-club-b4e0df4ed578da57dafe1be557b952aa3afe3dbd.tar.gz perlweeklychallenge-club-b4e0df4ed578da57dafe1be557b952aa3afe3dbd.tar.bz2 perlweeklychallenge-club-b4e0df4ed578da57dafe1be557b952aa3afe3dbd.zip | |
Merge pull request #1539 from Scimon/master
Further experiments with challenge 2
| -rw-r--r-- | challenge-055/simon-proctor/raku/ch-2-2.p6 | 31 | ||||
| -rw-r--r-- | challenge-055/simon-proctor/raku/ch-2-3.p6 | 14 |
2 files changed, 18 insertions, 27 deletions
diff --git a/challenge-055/simon-proctor/raku/ch-2-2.p6 b/challenge-055/simon-proctor/raku/ch-2-2.p6 index 327202b184..cec64a1e69 100644 --- a/challenge-055/simon-proctor/raku/ch-2-2.p6 +++ b/challenge-055/simon-proctor/raku/ch-2-2.p6 @@ -5,19 +5,17 @@ use v6.d; # Second attempt at this using a different plan #| Given a list of integers return all the wave sorted lists multi sub MAIN( *@input where { $_.elems >= 2 && $_.all ~~ Int } ) { - .join(",").say for find-waves( [], @input ); + .say for find-waves( [], @input ).race.map( { $_.join(",") } ); } # Start state multi sub find-waves( [], @input is copy ) { - my @res; - for @input.kv -> $i, $val { - my @poss = @input[0..^$i,$i^..*-1].flat; - for find-waves( [$val], @poss, 'gte' ).grep( { defined $_ } ) -> @r { - @res.push( @r ); + gather { + for @input.kv -> $i, $val { + my @poss = @input[0..^$i,$i^..*-1].flat; + take $_ for find-waves( [$val], @poss, 'gte' ).grep( { defined $_ } ); } } - return @res; } # End state @@ -30,17 +28,14 @@ multi sub find-waves( @i, @p where { @i[*-1] > all(@p) }, 'lte' ) {} # Possible State multi sub find-waves( @output is copy, @input is copy, $test ) { - my @out; - - my &test = $test ~~ 'gte' ?? &infix:«>=» !! &infix:«<=»; - - for @input.kv -> $i, $val { - next unless &test( @output[*-1], $val ); - my @poss = @input[0..^$i,$i^..*-1].flat; - - for find-waves( [|@output, $val], @poss, $test ~~ 'gte' ?? 'lte' !! 'gte' ).grep( { defined $_ } ) -> @r { - @out.push( @r ); + gather { + my &test = $test ~~ 'gte' ?? &infix:«>=» !! &infix:«<=»; + + for @input.kv -> $i, $val { + next unless &test( @output[*-1], $val ); + my @poss = @input[0..^$i,$i^..*-1].flat; + + take $_ for find-waves( [|@output, $val], @poss, $test ~~ 'gte' ?? 'lte' !! 'gte' ).grep( { defined $_ } ); } } - return @out; } diff --git a/challenge-055/simon-proctor/raku/ch-2-3.p6 b/challenge-055/simon-proctor/raku/ch-2-3.p6 index f8fe36709b..36344999c5 100644 --- a/challenge-055/simon-proctor/raku/ch-2-3.p6 +++ b/challenge-055/simon-proctor/raku/ch-2-3.p6 @@ -26,16 +26,12 @@ multi sub MAIN( *@input where { $_.elems >= 2 && $_.all ~~ Int } ) { $messages.done; } } - start react { - whenever $output-channel -> @out { - @out.join(",").say; - } + whenever $output-channel -> @out { + @out.join(",").say; + } + whenever $input-channel -> ( @i, @p, $t ) { + find-waves( @i, @p, $t, $input-channel, $output-channel, $messages ); } - start react { - whenever $input-channel -> ( @i, @p, $t ) { - find-waves( @i, @p, $t, $input-channel, $output-channel, $messages ); - } - } } } |
