Skip to content
Permalink
Browse files

feat: slidingWindow now returns an empty sequence if no=0

  • Loading branch information...
koraa committed Aug 27, 2019
1 parent bafa834 commit 533cff4ea539c54152c2f9eda8dfe4b50c83cccb
Showing with 15 additions and 1 deletion.
  1. +13 −1 src/sequence.js
  2. +2 −0 test/sequence.test.js
@@ -1448,12 +1448,18 @@ const zipLongest2 = curry('zipLongest2', (a, b, fallback) => zipLongest([a, b],
* Will throw IteratorEnded if the sequence is shorter than
* the given window.
*
* Returns an empty sequence if `no == 0`.
*
* @function
* @param {Sequence} seq A sequence of sequences
* @throws {IteratorEnded}
* @returns {Iterator} Iterator of lists
*/
const slidingWindow = curry('slidingWindow', (seq, no) => {
if (no === 0) {
return iter([]);
}

const it = iter(seq);
const cache = [];
each(range0(no), () => cache.push(next(it)));
@@ -1477,11 +1483,17 @@ const slidingWindow = curry('slidingWindow', (seq, no) => {
* Like slidingWindow, but returns an empty sequence if the given
* sequence is too short.
*
* Returns an empty sequence if `no == 0`.
*
* @function
* @param {Sequence} seq A sequence of sequences
* @returns {Iterator} Iterator of lists
*/
const trySlidingWindow = curry('trySlidingWindow', function* trySlidingWindow(seq, no) {
if (no === 0) {
return;
}

const it = iter(seq);
const cache = [];
for (let idx = 0; idx < no; idx += 1) {
@@ -1519,7 +1531,7 @@ const trySlidingWindow = curry('trySlidingWindow', function* trySlidingWindow(se
*
* @function
* @param {Sequence} seq
* @param {Number} no
* @param {Number} no Number of elements to look ahead to.
* @param {Any} filler
* @returns {Sequence<Array>}
*/
@@ -405,6 +405,8 @@ it('zipLongest', () => {

it('slidingWindow', () => {
each([slidingWindow, trySlidingWindow], (fn) => {
ckEqSeq(fn(0)([1, 2, 3]), []);
ckEqSeq(fn(0)([]), []);
ckEqSeq(fn(1)([1, 2, 3]), [[1], [2], [3]]);
ckEqSeq(fn(2)([1, 2, 3]), [[1, 2], [2, 3]]);
ckEqSeq(fn(3)([1, 2, 3]), [[1, 2, 3]]);

0 comments on commit 533cff4

Please sign in to comment.
You can’t perform that action at this time.