Last active
December 12, 2015 02:18
-
-
Save peczenyj/4697935 to your computer and use it in GitHub Desktop.
perl version of each_cons + a small helper function "of"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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