aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoray Jones <moray@mrjones.co.uk>2022-01-02 23:06:38 +0000
committerMoray Jones <moray@mrjones.co.uk>2022-01-02 23:06:38 +0000
commita768b6934d3bbf7a6a573f226ffdef255921696c (patch)
tree5aa7e7b072be934bf2bb8bfd715728cf4c349298
parentb3bd78828c9c0a169173683255a1bf92b6f3c0e8 (diff)
downloadperlweeklychallenge-club-a768b6934d3bbf7a6a573f226ffdef255921696c.tar.gz
perlweeklychallenge-club-a768b6934d3bbf7a6a573f226ffdef255921696c.tar.bz2
perlweeklychallenge-club-a768b6934d3bbf7a6a573f226ffdef255921696c.zip
Add dot challenge for 145
Adds answer for dot challenge
-rw-r--r--challenge-145/morayj/README1
-rw-r--r--challenge-145/morayj/raku/ch-1.raku39
2 files changed, 40 insertions, 0 deletions
diff --git a/challenge-145/morayj/README b/challenge-145/morayj/README
new file mode 100644
index 0000000000..1f63642b40
--- /dev/null
+++ b/challenge-145/morayj/README
@@ -0,0 +1 @@
+Solution by Moray Jones
diff --git a/challenge-145/morayj/raku/ch-1.raku b/challenge-145/morayj/raku/ch-1.raku
new file mode 100644
index 0000000000..f797812988
--- /dev/null
+++ b/challenge-145/morayj/raku/ch-1.raku
@@ -0,0 +1,39 @@
+my Int @a = (1,2,3,10);
+my Int @b = (3,4,3,2);
+
+# I saw the challenge and thought 'Z' the zip operator
+# Wanted to give it a go
+
+# First created a separate array with the zipped original arrays
+# This creates an array of 2 member lists
+
+my List @c = @a Z @b;
+
+# Tried a raku pointy block to unpack the pairs
+
+my Int $total = 0;
+for @c -> ($i, $j) {
+ $total = $total + ($i * $j);
+}
+say $total;
+
+# But could also use flat to flatten the lists
+# I like the 'reduction metaoperators' so use that
+# for the multiplication
+
+$total = 0;
+for @c {
+ $total = $total + ([*] flat $_);
+}
+say $total;
+
+# But then if using a loop, could use a map
+# Dispense with the @c array step
+# Can also use flat as a method on implied topic
+
+say [+] (@a Z @b).map: {([*] .flat)};
+
+# But checking the 'Z' documentation again
+# Z can do the map work itself it turns out
+
+say [+] @a Z* @b;