aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-07-27 20:03:50 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2020-07-30 17:55:53 +0200
commit1ff0e52d796f5ad1602bdf014df2650f98993217 (patch)
tree131eee895c90dd0ea976782403040a159c25c9e4
parent50331af61ed08f6ad91c085df4d1cc32016e18bd (diff)
downloadperlweeklychallenge-club-1ff0e52d796f5ad1602bdf014df2650f98993217.tar.gz
perlweeklychallenge-club-1ff0e52d796f5ad1602bdf014df2650f98993217.tar.bz2
perlweeklychallenge-club-1ff0e52d796f5ad1602bdf014df2650f98993217.zip
solution for task 1
-rwxr-xr-xchallenge-071/jo-37/perl/ch-1.pl36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-071/jo-37/perl/ch-1.pl b/challenge-071/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..c6a873d686
--- /dev/null
+++ b/challenge-071/jo-37/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use List::Util qw(shuffle);
+
+# map3 is some kind of mixture and extension of map and reduce:
+# It iterates over the elements of a list calling a code block,
+# locally setting
+# - $a to the previous element
+# - $_ to the current element
+# - $b to the next element
+# and returning the the results of each invocation of the code block.
+# The block is never called with $_ set to the first or last element
+# of the list.
+sub map3 (&@) {
+ my $code = shift;
+
+ my ($prev, $current, $next, $i);
+ map {
+ ($prev, $current, $next) = ($current, $next, $_);
+ ++$i > 2 ?
+ do {local ($a, $_, $b) = ($prev, $current, $next); $code->()} :
+ ();
+ } @_;
+}
+
+my $N = 10;
+local $" = ', ';
+
+# Shuffle the deck and take the first N cards.
+my @rand = splice @{[shuffle 1 .. 50]}, 0, $N;
+print "Array: [@rand]\n";
+
+# Add zeroes around the array to detect "border peaks".
+print "Peak: [@{[map3 {$_ > $a && $_ > $b ? $_ : ()} 0, @rand, 0]}]\n";