From fe5a370b07dd874d5eb5099858904c7ee5ac5e4c Mon Sep 17 00:00:00 2001 From: Daniel Mita Date: Sat, 21 Dec 2019 22:21:34 +0000 Subject: Refactor Raku challenge 039-1 --- challenge-039/daniel-mita/perl6/ch-1.p6 | 50 +++++++++++++++------------------ 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/challenge-039/daniel-mita/perl6/ch-1.p6 b/challenge-039/daniel-mita/perl6/ch-1.p6 index c956e7a455..75c3013bcb 100755 --- a/challenge-039/daniel-mita/perl6/ch-1.p6 +++ b/challenge-039/daniel-mita/perl6/ch-1.p6 @@ -16,36 +16,28 @@ constant @guest-list = ( { name => 'Neil', in => '10:01', out => '10:19' }, { name => 'Chris', in => '10:10', out => '11:00' }, ); +my %present is SetHash; # To contain @guest-list indexes -my Supplier $sign-in.=new; -my Supplier $sign-out.=new; - -my Supplier $lightswitch.=new; my Bool $light; - -start react whenever $lightswitch.Supply { +sub lightswitch ( --> Nil ) { "Light has been switched {$light.=not ?? 'on' !! 'off'}".say; } -start react { - my %present is SetHash; # To contain @guest-list indexes +sub sign-in ( $i --> Nil ) { + lightswitch unless $light; # Pretty dark, let's hit the switch + %present{$i}++; + "@guest-list[$i] has signed in".say; +} - whenever $sign-in.Supply { - $lightswitch.emit(Nil) unless $light; # Pretty dark, let's hit the switch - %present{$_}++; - "@guest-list[$_] has signed in".say; +sub sign-out ( $i --> Nil ) { + %present{$i}--; + "@guest-list[$i] has signed out".say; + if %present ~~ ∅ { + 'Looks like nobody is here'.say; + lightswitch; } - - whenever $sign-out.Supply { - %present{$_}--; - "@guest-list[$_] has signed out".say; - if %present ~~ ∅ { - 'Looks like nobody is here'.say; - $lightswitch.emit(Nil); - } - else { - say 'Leave light on for ' ~ %present.keys.map({ @guest-list[$_] }).join(', '); - } + else { + say 'Leave light on for ' ~ %present.keys.map({ @guest-list[$_] }).join(', '); } } @@ -58,13 +50,15 @@ loop ( $light-duration++ if $light; my $t = $d.hh-mm-ss.substr(0, *-3); # Don't care about seconds - my @in = @guest-list.pairs.grep(*.value eq $t).map(*.key); - my @out = @guest-list.pairs.grep(*.value eq $t).map(*.key); + my %sign; + for -> $k { + %sign{$k} = @guest-list.pairs.grep(*.value{$k} eq $t).map(*.key); + } - if @in.elems || @out.elems { + if %sign.any { "\n$t".say; - $sign-in.emit($_) for @in; - $sign-out.emit($_) for @out; + sign-in($_) for %sign .values; + sign-out($_) for %sign.values; } elsif $all-times { "\n$t: Nothing happened".say; -- cgit