Skip to content

Commit 533cff4

Browse files
committed
feat: slidingWindow now returns an empty sequence if no=0
1 parent bafa834 commit 533cff4

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/sequence.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1448,12 +1448,18 @@ const zipLongest2 = curry('zipLongest2', (a, b, fallback) => zipLongest([a, b],
14481448
* Will throw IteratorEnded if the sequence is shorter than
14491449
* the given window.
14501450
*
1451+
* Returns an empty sequence if `no == 0`.
1452+
*
14511453
* @function
14521454
* @param {Sequence} seq A sequence of sequences
14531455
* @throws {IteratorEnded}
14541456
* @returns {Iterator} Iterator of lists
14551457
*/
14561458
const slidingWindow = curry('slidingWindow', (seq, no) => {
1459+
if (no === 0) {
1460+
return iter([]);
1461+
}
1462+
14571463
const it = iter(seq);
14581464
const cache = [];
14591465
each(range0(no), () => cache.push(next(it)));
@@ -1477,11 +1483,17 @@ const slidingWindow = curry('slidingWindow', (seq, no) => {
14771483
* Like slidingWindow, but returns an empty sequence if the given
14781484
* sequence is too short.
14791485
*
1486+
* Returns an empty sequence if `no == 0`.
1487+
*
14801488
* @function
14811489
* @param {Sequence} seq A sequence of sequences
14821490
* @returns {Iterator} Iterator of lists
14831491
*/
14841492
const trySlidingWindow = curry('trySlidingWindow', function* trySlidingWindow(seq, no) {
1493+
if (no === 0) {
1494+
return;
1495+
}
1496+
14851497
const it = iter(seq);
14861498
const cache = [];
14871499
for (let idx = 0; idx < no; idx += 1) {
@@ -1519,7 +1531,7 @@ const trySlidingWindow = curry('trySlidingWindow', function* trySlidingWindow(se
15191531
*
15201532
* @function
15211533
* @param {Sequence} seq
1522-
* @param {Number} no
1534+
* @param {Number} no Number of elements to look ahead to.
15231535
* @param {Any} filler
15241536
* @returns {Sequence<Array>}
15251537
*/

test/sequence.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ it('zipLongest', () => {
405405

406406
it('slidingWindow', () => {
407407
each([slidingWindow, trySlidingWindow], (fn) => {
408+
ckEqSeq(fn(0)([1, 2, 3]), []);
409+
ckEqSeq(fn(0)([]), []);
408410
ckEqSeq(fn(1)([1, 2, 3]), [[1], [2], [3]]);
409411
ckEqSeq(fn(2)([1, 2, 3]), [[1, 2], [2, 3]]);
410412
ckEqSeq(fn(3)([1, 2, 3]), [[1, 2, 3]]);

0 commit comments

Comments
 (0)