aboutsummaryrefslogtreecommitdiff
path: root/challenge-034/arne-sommer
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-11-14 14:59:11 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-11-14 14:59:11 +0000
commitebec20e615a039f7c00ee9f290feedc1e1f110ab (patch)
tree99665a45675c5fe79a5abf6bf1719cbb84d4b828 /challenge-034/arne-sommer
parent51ec5a93cbf273002a923569929b158cb7ed8de5 (diff)
downloadperlweeklychallenge-club-ebec20e615a039f7c00ee9f290feedc1e1f110ab.tar.gz
perlweeklychallenge-club-ebec20e615a039f7c00ee9f290feedc1e1f110ab.tar.bz2
perlweeklychallenge-club-ebec20e615a039f7c00ee9f290feedc1e1f110ab.zip
- Added solutions by Arne Sommer.
Diffstat (limited to 'challenge-034/arne-sommer')
-rw-r--r--challenge-034/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-034/arne-sommer/perl6/ch-1.p613
-rwxr-xr-xchallenge-034/arne-sommer/perl6/ch-2.p654
-rwxr-xr-xchallenge-034/arne-sommer/perl6/dispatch54
-rwxr-xr-xchallenge-034/arne-sommer/perl6/dispatch-anon20
-rwxr-xr-xchallenge-034/arne-sommer/perl6/dispatch-anon-set20
-rwxr-xr-xchallenge-034/arne-sommer/perl6/pre-dispatch31
-rwxr-xr-xchallenge-034/arne-sommer/perl6/pre-dispatch-sub40
-rwxr-xr-xchallenge-034/arne-sommer/perl6/pre-dispatch-sub-given41
-rwxr-xr-xchallenge-034/arne-sommer/perl6/slice-n-dice13
10 files changed, 287 insertions, 0 deletions
diff --git a/challenge-034/arne-sommer/blog.txt b/challenge-034/arne-sommer/blog.txt
new file mode 100644
index 0000000000..f56e7a7bbe
--- /dev/null
+++ b/challenge-034/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/sliced-dispatch.html
diff --git a/challenge-034/arne-sommer/perl6/ch-1.p6 b/challenge-034/arne-sommer/perl6/ch-1.p6
new file mode 100755
index 0000000000..5bddd857ad
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/ch-1.p6
@@ -0,0 +1,13 @@
+#! /usr/bin/env raku
+
+my @values = <zero one twice thrice four fifth VI seventh acht nine X>;
+
+say @values[0 .. 10];
+say @values[0 .. 12];
+say @values[10 ... 0];
+say @values[7, 4, 1];
+
+my %values = @values.antipairs;
+
+say %values<zero>;
+say %values<zero VI nine>;
diff --git a/challenge-034/arne-sommer/perl6/ch-2.p6 b/challenge-034/arne-sommer/perl6/ch-2.p6
new file mode 100755
index 0000000000..87548d6687
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/ch-2.p6
@@ -0,0 +1,54 @@
+#! /usr/bin/env raku
+
+my $value = 0;
+
+my %dispatch =
+(
+ q => &last,
+ quit => &last,
+ d => &double,
+ double => &double,
+ h => &half,
+ half => &half,
+ c => &clear,
+ clear => &clear,
+ help => &help,
+);
+
+loop
+{
+ my $command = prompt "[$value]: ";
+
+ if %dispatch{$command} { %dispatch{$command}(); }
+ elsif $command ~~ /^(\d)$/ { set($0.Int); }
+}
+
+sub last
+{
+ exit;
+}
+
+sub double
+{
+ $value *= 2;
+}
+
+sub half
+{
+ $value /= 2;
+}
+
+sub clear
+{
+ $value = 0;
+}
+
+sub set ($new)
+{
+ $value = $new;
+}
+
+sub help
+{
+ say "Please consult a doctor.";
+}
diff --git a/challenge-034/arne-sommer/perl6/dispatch b/challenge-034/arne-sommer/perl6/dispatch
new file mode 100755
index 0000000000..87548d6687
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/dispatch
@@ -0,0 +1,54 @@
+#! /usr/bin/env raku
+
+my $value = 0;
+
+my %dispatch =
+(
+ q => &last,
+ quit => &last,
+ d => &double,
+ double => &double,
+ h => &half,
+ half => &half,
+ c => &clear,
+ clear => &clear,
+ help => &help,
+);
+
+loop
+{
+ my $command = prompt "[$value]: ";
+
+ if %dispatch{$command} { %dispatch{$command}(); }
+ elsif $command ~~ /^(\d)$/ { set($0.Int); }
+}
+
+sub last
+{
+ exit;
+}
+
+sub double
+{
+ $value *= 2;
+}
+
+sub half
+{
+ $value /= 2;
+}
+
+sub clear
+{
+ $value = 0;
+}
+
+sub set ($new)
+{
+ $value = $new;
+}
+
+sub help
+{
+ say "Please consult a doctor.";
+}
diff --git a/challenge-034/arne-sommer/perl6/dispatch-anon b/challenge-034/arne-sommer/perl6/dispatch-anon
new file mode 100755
index 0000000000..4abc4d5f90
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/dispatch-anon
@@ -0,0 +1,20 @@
+#! /usr/bin/env raku
+
+my $value = 0;
+
+my %dispatch;
+
+%dispatch<q> = %dispatch<quit> = { exit };
+%dispatch<d> = %dispatch<double> = { $value *= 2 };
+%dispatch<h> = %dispatch<half> = { $value /= 2 };
+%dispatch<c> = %dispatch<clear> = { $value = 0 };
+%dispatch<help> = { say "Please consult a doctor." };
+
+loop
+{
+ my $command = prompt "[$value]: ";
+
+ if %dispatch{$command} { %dispatch{$command}(); }
+ elsif $command ~~ /^(\d)$/ { $value = $0.Int; }
+}
+
diff --git a/challenge-034/arne-sommer/perl6/dispatch-anon-set b/challenge-034/arne-sommer/perl6/dispatch-anon-set
new file mode 100755
index 0000000000..a9c39e8bea
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/dispatch-anon-set
@@ -0,0 +1,20 @@
+#! /usr/bin/env raku
+
+my $value = 0;
+
+my %dispatch;
+
+%dispatch<q> = %dispatch<quit> = { exit };
+%dispatch<d> = %dispatch<double> = { $value *= 2 };
+%dispatch<h> = %dispatch<half> = { $value /= 2 };
+%dispatch<c> = %dispatch<clear> = { $value = 0 };
+%dispatch<s> = %dispatch<set> = { $value = $0.Int if @_[0] ~~ /^(\d)$/ };
+%dispatch<help> = { say "Please consult a doctor." };
+
+loop
+{
+ my @command = (prompt "[$value]: ").words;
+
+ if %dispatch{@command[0]} { @command[1] ?? %dispatch{@command[0]}(@command[1]) !! %dispatch{@command[0]}() }
+}
+
diff --git a/challenge-034/arne-sommer/perl6/pre-dispatch b/challenge-034/arne-sommer/perl6/pre-dispatch
new file mode 100755
index 0000000000..59549b97bc
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/pre-dispatch
@@ -0,0 +1,31 @@
+#! /usr/bin/env raku
+
+my $value = 0;
+
+loop
+{
+ my $command = prompt "[$value]: ";
+
+ last if $command eq "q" | "quit";
+
+ if $command eq "d" | "double"
+ {
+ $value *= 2;
+ }
+ elsif $command eq "h" | "half"
+ {
+ $value /= 2;
+ }
+ elsif $command eq "c" | "clear"
+ {
+ $value = 0;
+ }
+ elsif $command ~~ /^(\d)$/
+ {
+ $value = $0.Int;
+ }
+ elsif $command eq "help"
+ {
+ say "Please consult a doctor.";
+ }
+}
diff --git a/challenge-034/arne-sommer/perl6/pre-dispatch-sub b/challenge-034/arne-sommer/perl6/pre-dispatch-sub
new file mode 100755
index 0000000000..a0a0305ea0
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/pre-dispatch-sub
@@ -0,0 +1,40 @@
+#! /usr/bin/env raku
+
+my $value = 0;
+
+loop
+{
+ my $command = prompt "[$value]: ";
+
+ if $command eq "q" | "quit" { last; }
+ elsif $command eq "d" | "double" { double; }
+ elsif $command eq "h" | "half" { half; }
+ elsif $command eq "c" | "clear" { clear; }
+ elsif $command ~~ /^(\d)$/ { set($0.Int); }
+ elsif $command eq "help" { help; }
+}
+
+sub double
+{
+ $value *= 2;
+}
+
+sub half
+{
+ $value /= 2;
+}
+
+sub clear
+{
+ $value = 0;
+}
+
+sub set ($new)
+{
+ $value = $new;
+}
+
+sub help
+{
+ say "Please consult a doctor.";
+}
diff --git a/challenge-034/arne-sommer/perl6/pre-dispatch-sub-given b/challenge-034/arne-sommer/perl6/pre-dispatch-sub-given
new file mode 100755
index 0000000000..225f531059
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/pre-dispatch-sub-given
@@ -0,0 +1,41 @@
+#! /usr/bin/env raku
+
+my $value = 0;
+
+loop
+{
+ given prompt "[$value]: "
+ {
+ when "q" | "quit" { last; }
+ when "d" | "double" { double; }
+ when "h" | "half" { half; }
+ when "c" | "clear" { clear; }
+ when /^(\d)$/ { set($0.Int); }
+ when "help" { help; }
+ }
+}
+
+sub double
+{
+ $value *= 2;
+}
+
+sub half
+{
+ $value /= 2;
+}
+
+sub clear
+{
+ $value = 0;
+}
+
+sub set ($new)
+{
+ $value = $new;
+}
+
+sub help
+{
+ say "Please consult a doctor.";
+}
diff --git a/challenge-034/arne-sommer/perl6/slice-n-dice b/challenge-034/arne-sommer/perl6/slice-n-dice
new file mode 100755
index 0000000000..5bddd857ad
--- /dev/null
+++ b/challenge-034/arne-sommer/perl6/slice-n-dice
@@ -0,0 +1,13 @@
+#! /usr/bin/env raku
+
+my @values = <zero one twice thrice four fifth VI seventh acht nine X>;
+
+say @values[0 .. 10];
+say @values[0 .. 12];
+say @values[10 ... 0];
+say @values[7, 4, 1];
+
+my %values = @values.antipairs;
+
+say %values<zero>;
+say %values<zero VI nine>;