aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-055/simon-proctor/raku/ch-2-2.p631
-rw-r--r--challenge-055/simon-proctor/raku/ch-2-3.p614
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 );
- }
- }
}
}