diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-03 02:54:39 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-03 02:54:39 +0000 |
| commit | 708f6aab541613e5bde9e0d81b1eda45a939e9ae (patch) | |
| tree | 523dd161f7b10bd1ab29cd9525ad5887e0f17ad7 | |
| parent | c4730c3acbe8610ed9cf8d3cc799d15d4bc0ae44 (diff) | |
| parent | a768b6934d3bbf7a6a573f226ffdef255921696c (diff) | |
| download | perlweeklychallenge-club-708f6aab541613e5bde9e0d81b1eda45a939e9ae.tar.gz perlweeklychallenge-club-708f6aab541613e5bde9e0d81b1eda45a939e9ae.tar.bz2 perlweeklychallenge-club-708f6aab541613e5bde9e0d81b1eda45a939e9ae.zip | |
Merge pull request #5452 from MorayJ/mj145
Add dot challenge for 145
| -rw-r--r-- | challenge-145/morayj/README | 1 | ||||
| -rw-r--r-- | challenge-145/morayj/raku/ch-1.raku | 39 |
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; |
