diff options
| author | Simon Proctor <simon.proctor@zpg.co.uk> | 2019-11-11 09:56:52 +0000 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zpg.co.uk> | 2019-11-11 09:56:52 +0000 |
| commit | 0e5e69f683fd7f8ba46b82bac3d8ce58c7f61aa8 (patch) | |
| tree | 2d04aa2549c52932d1663d9df0220a00c14430cd /challenge-034 | |
| parent | b8e4b2a64c88036e70a16f9a986165c81dd18b1f (diff) | |
| download | perlweeklychallenge-club-0e5e69f683fd7f8ba46b82bac3d8ce58c7f61aa8.tar.gz perlweeklychallenge-club-0e5e69f683fd7f8ba46b82bac3d8ce58c7f61aa8.tar.bz2 perlweeklychallenge-club-0e5e69f683fd7f8ba46b82bac3d8ce58c7f61aa8.zip | |
Despatch Table
Diffstat (limited to 'challenge-034')
| -rw-r--r-- | challenge-034/simon-proctor/perl6/ch-2.p6 | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-034/simon-proctor/perl6/ch-2.p6 b/challenge-034/simon-proctor/perl6/ch-2.p6 new file mode 100644 index 0000000000..eae6b2131f --- /dev/null +++ b/challenge-034/simon-proctor/perl6/ch-2.p6 @@ -0,0 +1,32 @@ +#!/usr/bin/env perl6 + +use v6; + +# Possible actions +sub doubler ( Int $x ) { $x * 2 } +sub halver ( Int $x ) { $x / 2 } +sub squarer ( Int $x ) { $x * $x } +sub reverser ( Int $x ) { $x.flip.Int } +sub zeroer ( Int $x ) { 0 } + +# Despatch table +my %commands = ( + "double" => &doubler, + "halve" => &halver, + "square" => &squarer, + "reverse" => &reverser, + "zero" => &zeroer, +); + +#| Help +multi sub MAIN( Bool :h(:$help) where so * ) { + say $*USAGE; +} + +#| Use despatch table to run the given command +multi sub MAIN( + Str $command where { %commands{$command}:exists }, #= Valid command from double, halve, square, reverse, zero + Int() $value #= Value to apply command to +) { + say "{$command}( {$value} ) => {%commands{$command}($value)}"; +} |
