Skip to content

Instantly share code, notes, and snippets.

@peczenyj
Last active December 12, 2015 02:18
Show Gist options
  • Select an option

  • Save peczenyj/4697935 to your computer and use it in GitHub Desktop.

Select an option

Save peczenyj/4697935 to your computer and use it in GitHub Desktop.
perl version of each_cons + a small helper function "of"
use strict;
use warnings;
use English;
use feature 'say';
sub of { [ @_ ] }
sub each_cons(&$$) {
my ($code, $p, $array) = @ARG;
my $n = scalar @{$array};
foreach my $i ( 0 .. $n - $p) {
$code->(@{$array}[$i .. $i + $p - 1])
}
}
each_cons { say "@ARG" } 3 => of 1 .. 10;
__END__
will print
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
8 9 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment