consecutive
优质
小牛编辑
146浏览
2023-12-01
Groups this sequence into consecutive (overlapping) segments of a specified length. If the underlying sequence has fewer elements than the specified length, then this sequence will be empty.
Signature
Sequence.consecutive = function(length) { /*...*/ }
Sequence.consecutive = function consecutive(count) { return new ConsecutiveSequence(this, count); }
Name | Type(s) | Description |
---|---|---|
length | number | The length of each consecutive segment. |
returns | Sequence | The resulting sequence of consecutive segments. |
Examples
function sum(vals) { return Lazy(vals).sum(); } var pairs = Lazy([1, 2, 3, 4]).consecutive(2); // Make sure consecutive sequences are reusable. pairs.map(sum) // => sequence: [3, 5, 7] pairs.map(sum) // => sequence: [3, 5, 7] Lazy([]).consecutive(2) // => sequence: [] Lazy([1]).consecutive(2) // => sequence: [] Lazy([1, 2]).consecutive(2) // => sequence: [[1, 2]] Lazy([1, 2, 3]).consecutive(2) // => sequence: [[1, 2], [2, 3]] Lazy([1, 2, 3]).consecutive(0) // => sequence: [[]] Lazy([1, 2, 3]).consecutive(1) // => sequence: [[1], [2], [3]]