diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-16 10:23:31 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-16 10:23:31 +0000 |
| commit | 1d677c32475eeb9d509076cb14cfcb1b8d0846a9 (patch) | |
| tree | 58ce3ebeef447d19d5eb026e8daefc1be2b83da7 | |
| parent | 32c065869c9f794121ce5ead73ac7044acb70b36 (diff) | |
| parent | c10fbc00031541e40fda1b4f5923592008618039 (diff) | |
| download | perlweeklychallenge-club-1d677c32475eeb9d509076cb14cfcb1b8d0846a9.tar.gz perlweeklychallenge-club-1d677c32475eeb9d509076cb14cfcb1b8d0846a9.tar.bz2 perlweeklychallenge-club-1d677c32475eeb9d509076cb14cfcb1b8d0846a9.zip | |
Merge pull request #5522 from ccntrq/challenge-145
Add solution for challenge 145 task 1
| -rw-r--r-- | challenge-145/alexander-pankoff/perl/ch-1.pl | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-145/alexander-pankoff/perl/ch-1.pl b/challenge-145/alexander-pankoff/perl/ch-1.pl new file mode 100644 index 0000000000..cdb049522e --- /dev/null +++ b/challenge-145/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw'say signatures'; +no warnings qw'experimental::signatures'; + +use Data::Dumper; +use List::Util qw(sum0 min); + +run() unless caller(); + +sub run() { + my @a = ( 1, 2, 3 ); + my @b = ( 4, 5, 6 ); + + my $dot_product = dot_product( \@a, \@b ); + + say $dot_product; + +} + +sub dot_product ( $a, $b ) { + sum0( zip_with( sub ( $a, $b ) { $a * $b }, $a, $b ) ); + +} + +sub zip_with ( $op, $as, $bs ) { + map { $op->($as->[$_], $bs->[$_] ) } 0 .. min( $#{$as}, $#{$bs} ); +} + |
