Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tweaks to sliding-window based on TimToady and pmichaud's suggestions…

…, and additional tests for it.
  • Loading branch information...
commit 61384ec65a151b003391e6d57f71afa2212f9d3d 1 parent 506321f
@colomon authored
Showing with 12 additions and 6 deletions.
  1. +2 −6 lib/List/Utils.pm
  2. +10 −0 t/01-sliding-window.t
View
8 lib/List/Utils.pm
@@ -1,16 +1,12 @@
module List::Utils;
sub sliding-window(@a, $n) is export(:DEFAULT) {
- my $a-list = @a.list;
+ my $a-list = @a.iterator.list;
my @values;
gather while my $a = $a-list.shift {
@values.push($a);
@values.shift if +@values > $n;
- if +@values == $n {
- for @values -> $value {
- take $value;
- }
- }
+ take @values if +@values == $n;
}
}
View
10 t/01-sliding-window.t
@@ -3,6 +3,7 @@ use List::Utils;
plan *;
+
is ~sliding-window((1, 2, 3), 1), ~(1, 2, 3), "one at a time works (parcel)";
is ~sliding-window(1..3, 1), ~(1, 2, 3), "one at a time works (range)";
# is ~sliding-window(1...3, 1), ~(1, 2, 3), "one at a time works (series)";
@@ -13,4 +14,13 @@ is ~sliding-window(1..5, 2), ~(1, 2, 2, 3, 3, 4, 4, 5), "two at a time works (ra
is ~sliding-window((1, 2, 3, 4, 5), 3), ~(1, 2, 3, 2, 3, 4, 3, 4, 5), "three at a time works (parcel)";
is ~sliding-window(1..5, 3), ~(1, 2, 3, 2, 3, 4, 3, 4, 5), "three at a time works (range)";
+{
+ my @a = 1..5;
+ is ~sliding-window(@a, 2), ~(1, 2, 2, 3, 3, 4, 4, 5), "two at a time works on Array";
+ is ~@a, (1, 2, 3, 4, 5), "Original array still okay";
+}
+
+is ~sliding-window(([1, 'a'], [2, 'b'], [3, 'c']), 2), ~(([1, 'a'], [2, 'b']), ([2, 'b'], [3, 'c'])),
+ "sliding preserves internal structure";
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.