diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-12-21 22:27:01 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-21 22:27:01 +0000 |
| commit | 7074b998d92e8c3f919c06c2dac22fb410060aaf (patch) | |
| tree | 7f4a0ca11fa2a0caada719505bf29922ae9d16b1 | |
| parent | cf00246056e076204b360711f75055dbdd2c1d67 (diff) | |
| parent | fe5a370b07dd874d5eb5099858904c7ee5ac5e4c (diff) | |
| download | perlweeklychallenge-club-7074b998d92e8c3f919c06c2dac22fb410060aaf.tar.gz perlweeklychallenge-club-7074b998d92e8c3f919c06c2dac22fb410060aaf.tar.bz2 perlweeklychallenge-club-7074b998d92e8c3f919c06c2dac22fb410060aaf.zip | |
Merge pull request #1055 from mienaikage/039-1-raku-refactor
Refactor Raku challenge 039-1
| -rwxr-xr-x | challenge-039/daniel-mita/perl6/ch-1.p6 | 50 |
1 files 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]<name> has signed in".say; +} - whenever $sign-in.Supply { - $lightswitch.emit(Nil) unless $light; # Pretty dark, let's hit the switch - %present{$_}++; - "@guest-list[$_]<name> has signed in".say; +sub sign-out ( $i --> Nil ) { + %present{$i}--; + "@guest-list[$i]<name> has signed out".say; + if %present ~~ ∅ { + 'Looks like nobody is here'.say; + lightswitch; } - - whenever $sign-out.Supply { - %present{$_}--; - "@guest-list[$_]<name> 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[$_]<name> }).join(', '); - } + else { + say 'Leave light on for ' ~ %present.keys.map({ @guest-list[$_]<name> }).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<in> eq $t).map(*.key); - my @out = @guest-list.pairs.grep(*.value<out> eq $t).map(*.key); + my %sign; + for <in out> -> $k { + %sign{$k} = @guest-list.pairs.grep(*.value{$k} eq $t).map(*.key); + } - if @in.elems || @out.elems { + if %sign<in out>.any { "\n$t".say; - $sign-in.emit($_) for @in; - $sign-out.emit($_) for @out; + sign-in($_) for %sign<in> .values; + sign-out($_) for %sign<out>.values; } elsif $all-times { "\n$t: Nothing happened".say; |
