Skip to content

Commit

Permalink
Use IterableIterator as return type for all generators
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Aug 14, 2022
1 parent 222502b commit ec648c0
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/algorithm/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Array.from(stream); // [1, 2, 3, 4, 5, 6]
* ```
*/
export function* chain<T>(...objects: Iterable<T>[]) {
export function* chain<T>(...objects: Iterable<T>[]): IterableIterator<T> {
for (const object of objects) {
for (const value of object) {
yield value;
Expand Down
2 changes: 1 addition & 1 deletion packages/algorithm/src/empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
* ```
*/
// eslint-disable-next-line require-yield, @typescript-eslint/no-unused-vars
export function* empty<T>() {
export function* empty<T>(): IterableIterator<T> {
return;
}
5 changes: 4 additions & 1 deletion packages/algorithm/src/enumerate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
* Array.from(stream); // [[1, 'foo'], [2, 'bar'], [3, 'baz']]
* ```
*/
export function* enumerate<T>(object: Iterable<T>, start = 0) {
export function* enumerate<T>(
object: Iterable<T>,
start = 0
): IterableIterator<[number, T]> {
for (const value of object) {
yield [start++, value];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/algorithm/src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
export function* filter<T>(
object: Iterable<T>,
fn: (value: T, index: number) => boolean
) {
): IterableIterator<T> {
let index = 0;
for (const value of object) {
if (fn(value, index++)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/algorithm/src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
export function* map<T, U>(
object: Iterable<T>,
fn: (value: T, index: number) => U
) {
): IterableIterator<U> {
let index = 0;
for (const value of object) {
yield fn(value, index++);
Expand Down
6 changes: 5 additions & 1 deletion packages/algorithm/src/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
if (stop === undefined) {
stop = start;
start = 0;
Expand Down
4 changes: 2 additions & 2 deletions packages/algorithm/src/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Array.from(stream); // [7, 7, 7]
* ```
*/
export function* repeat<T>(value: T, count: number) {
export function* repeat<T>(value: T, count: number): IterableIterator<T> {
while (0 < count--) {
yield value;
}
Expand All @@ -48,6 +48,6 @@ export function* repeat<T>(value: T, count: number) {
* Array.from(stream); // [7]
* ```
*/
export function* once<T>(value: T) {
export function* once<T>(value: T): IterableIterator<T> {
yield value;
}
5 changes: 4 additions & 1 deletion packages/algorithm/src/stride.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
* Array.from(stream); // [1, 3, 5];
* ```
*/
export function* stride<T>(object: Iterable<T>, step: number) {
export function* stride<T>(
object: Iterable<T>,
step: number
): IterableIterator<T> {
let count = 0;
for (const value of object) {
if (0 === count++ % step) {
Expand Down
5 changes: 4 additions & 1 deletion packages/algorithm/src/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
* Array.from(stream); // [5, 4, 3]
* ```
*/
export function* take<T>(object: Iterable<T>, count: number) {
export function* take<T>(
object: Iterable<T>,
count: number
): IterableIterator<T> {
for (const value of object) {
if (0 < count--) {
yield value;
Expand Down
2 changes: 1 addition & 1 deletion packages/algorithm/src/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { every } from './iter';
* Array.from(stream); // [[1, 4], [2, 5], [3, 6]]
* ```
*/
export function* zip<T>(...objects: Iterable<T>[]) {
export function* zip<T>(...objects: Iterable<T>[]): IterableIterator<T[]> {
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())) {
Expand Down

0 comments on commit ec648c0

Please sign in to comment.