From ec648c0863614ce1c84513aa7f710821ea984af2 Mon Sep 17 00:00:00 2001 From: "Afshin T. Darian" Date: Mon, 15 Aug 2022 00:45:19 +0100 Subject: [PATCH] Use `IterableIterator` as return type for all generators --- packages/algorithm/src/chain.ts | 2 +- packages/algorithm/src/empty.ts | 2 +- packages/algorithm/src/enumerate.ts | 5 ++++- packages/algorithm/src/filter.ts | 2 +- packages/algorithm/src/map.ts | 2 +- packages/algorithm/src/range.ts | 6 +++++- packages/algorithm/src/repeat.ts | 4 ++-- packages/algorithm/src/stride.ts | 5 ++++- packages/algorithm/src/take.ts | 5 ++++- packages/algorithm/src/zip.ts | 2 +- 10 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/algorithm/src/chain.ts b/packages/algorithm/src/chain.ts index 20db1c872..18e6844c7 100644 --- a/packages/algorithm/src/chain.ts +++ b/packages/algorithm/src/chain.ts @@ -28,7 +28,7 @@ * Array.from(stream); // [1, 2, 3, 4, 5, 6] * ``` */ -export function* chain(...objects: Iterable[]) { +export function* chain(...objects: Iterable[]): IterableIterator { for (const object of objects) { for (const value of object) { yield value; diff --git a/packages/algorithm/src/empty.ts b/packages/algorithm/src/empty.ts index a7c580382..2bb1c3d19 100644 --- a/packages/algorithm/src/empty.ts +++ b/packages/algorithm/src/empty.ts @@ -23,6 +23,6 @@ * ``` */ // eslint-disable-next-line require-yield, @typescript-eslint/no-unused-vars -export function* empty() { +export function* empty(): IterableIterator { return; } diff --git a/packages/algorithm/src/enumerate.ts b/packages/algorithm/src/enumerate.ts index 9765ff614..6ab1347be 100644 --- a/packages/algorithm/src/enumerate.ts +++ b/packages/algorithm/src/enumerate.ts @@ -28,7 +28,10 @@ * Array.from(stream); // [[1, 'foo'], [2, 'bar'], [3, 'baz']] * ``` */ -export function* enumerate(object: Iterable, start = 0) { +export function* enumerate( + object: Iterable, + start = 0 +): IterableIterator<[number, T]> { for (const value of object) { yield [start++, value]; } diff --git a/packages/algorithm/src/filter.ts b/packages/algorithm/src/filter.ts index 2eee493f9..e0d9ac873 100644 --- a/packages/algorithm/src/filter.ts +++ b/packages/algorithm/src/filter.ts @@ -31,7 +31,7 @@ export function* filter( object: Iterable, fn: (value: T, index: number) => boolean -) { +): IterableIterator { let index = 0; for (const value of object) { if (fn(value, index++)) { diff --git a/packages/algorithm/src/map.ts b/packages/algorithm/src/map.ts index 575416993..59469e396 100644 --- a/packages/algorithm/src/map.ts +++ b/packages/algorithm/src/map.ts @@ -30,7 +30,7 @@ export function* map( object: Iterable, fn: (value: T, index: number) => U -) { +): IterableIterator { let index = 0; for (const value of object) { yield fn(value, index++); diff --git a/packages/algorithm/src/range.ts b/packages/algorithm/src/range.ts index cc314e31a..e3df6c3c1 100644 --- a/packages/algorithm/src/range.ts +++ b/packages/algorithm/src/range.ts @@ -34,7 +34,11 @@ * Array.from(stream); // [2, 3] * ``` */ -export function* range(start: number, stop?: number, step?: number) { +export function* range( + start: number, + stop?: number, + step?: number +): IterableIterator { if (stop === undefined) { stop = start; start = 0; diff --git a/packages/algorithm/src/repeat.ts b/packages/algorithm/src/repeat.ts index a2869cbd9..499736713 100644 --- a/packages/algorithm/src/repeat.ts +++ b/packages/algorithm/src/repeat.ts @@ -26,7 +26,7 @@ * Array.from(stream); // [7, 7, 7] * ``` */ -export function* repeat(value: T, count: number) { +export function* repeat(value: T, count: number): IterableIterator { while (0 < count--) { yield value; } @@ -48,6 +48,6 @@ export function* repeat(value: T, count: number) { * Array.from(stream); // [7] * ``` */ -export function* once(value: T) { +export function* once(value: T): IterableIterator { yield value; } diff --git a/packages/algorithm/src/stride.ts b/packages/algorithm/src/stride.ts index c4babd5b4..e32e385ef 100644 --- a/packages/algorithm/src/stride.ts +++ b/packages/algorithm/src/stride.ts @@ -29,7 +29,10 @@ * Array.from(stream); // [1, 3, 5]; * ``` */ -export function* stride(object: Iterable, step: number) { +export function* stride( + object: Iterable, + step: number +): IterableIterator { let count = 0; for (const value of object) { if (0 === count++ % step) { diff --git a/packages/algorithm/src/take.ts b/packages/algorithm/src/take.ts index 021408017..f62b3a302 100644 --- a/packages/algorithm/src/take.ts +++ b/packages/algorithm/src/take.ts @@ -31,7 +31,10 @@ * Array.from(stream); // [5, 4, 3] * ``` */ -export function* take(object: Iterable, count: number) { +export function* take( + object: Iterable, + count: number +): IterableIterator { for (const value of object) { if (0 < count--) { yield value; diff --git a/packages/algorithm/src/zip.ts b/packages/algorithm/src/zip.ts index c3f09fe7b..c426b2b86 100644 --- a/packages/algorithm/src/zip.ts +++ b/packages/algorithm/src/zip.ts @@ -30,7 +30,7 @@ import { every } from './iter'; * Array.from(stream); // [[1, 4], [2, 5], [3, 6]] * ``` */ -export function* zip(...objects: Iterable[]) { +export function* zip(...objects: Iterable[]): IterableIterator { const iters = objects.map(obj => obj[Symbol.iterator]()); let tuple = iters.map(it => it.next()); for (; every(tuple, item => !item.done); tuple = iters.map(it => it.next())) {