Skip to content

Commit

Permalink
Fix islice regression (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie authored Jan 4, 2024
1 parent 0719482 commit 236fde4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [Unreleased]

- Fix `islice()` regression where it wasn't stopping on infinite iterables
(thanks for finding, @Kareem-Medhat 🙏!)

## v2.2.0

- Move to ESM by default
Expand Down
32 changes: 7 additions & 25 deletions src/itertools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,6 @@ import { primitiveIdentity } from "./utils";

const SENTINEL = Symbol();

function composeAnd(f1: (v1: number) => boolean, f2: (v2: number) => boolean): (v3: number) => boolean {
return (n: number) => f1(n) && f2(n);
}

function slicePredicate(start: number, stop: number | null, step: number) {
if (start < 0) throw new Error("start cannot be negative");
if (stop !== null && stop < 0) throw new Error("stop cannot be negative");
if (step <= 0) throw new Error("step cannot be negative");

let pred = (n: number) => n >= start;

if (stop !== null) {
const definedStop = stop;
pred = composeAnd(pred, (n: number) => n < definedStop);
}

if (step > 1) {
pred = composeAnd(pred, (n: number) => (n - start) % step === 0);
}

return pred;
}

/**
* Returns an iterator that returns elements from the first iterable until it
* is exhausted, then proceeds to the next iterable, until all of the iterables
Expand Down Expand Up @@ -201,9 +178,14 @@ export function* islice<T>(
stop = stopOrStart;
}

const pred = slicePredicate(start, stop, step);
if (start < 0) throw new Error("start cannot be negative");
if (stop !== null && stop < 0) throw new Error("stop cannot be negative");
if (step <= 0) throw new Error("step cannot be negative");

for (const [i, value] of enumerate(iterable)) {
if (pred(i)) {
if (i < start) continue;
if (stop !== null && i >= stop) break;
if ((i - start) % step === 0) {
yield value;
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/itertools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ describe("islice", () => {
expect(Array.from(islice("ABCDEFG", /*start*/ 1, /*stop*/ null, /*step*/ 2))).toEqual(["B", "D", "F"]);
});

it("islice with infinite inputs", () => {
expect(Array.from(islice(count(1), 0))).toEqual([]);
expect(Array.from(islice(count(1), 0, 0))).toEqual([]);
expect(Array.from(islice(count(1), 3, 2))).toEqual([]);
expect(Array.from(islice(count(1), 5))).toEqual([1, 2, 3, 4, 5]);
expect(Array.from(islice(count(1), 3, 7))).toEqual([4, 5, 6, 7]);
expect(Array.from(islice(count(1), 4, 32, 5))).toEqual([5, 10, 15, 20, 25, 30]);
expect(Array.from(islice(count(69), 18, 391, 81))).toEqual([87, 168, 249, 330, 411]);
});

it("islice invalid stop argument", () => {
expect(() => Array.from(islice("ABCDEFG", /*stop*/ -2))).toThrow();
expect(() => Array.from(islice("ABCDEFG", -2, -3))).toThrow();
Expand Down

0 comments on commit 236fde4

Please sign in to comment.