Files
pybricks-code/src/utils/iter.ts
T
2020-05-22 19:54:48 -05:00

23 lines
541 B
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
/**
* Creates an iterator that counts infinitely (to Number.MAX_SAFE_INTEGER really)
* starting from 0.
*/
export function* count(): Generator<number, number, void> {
let n = 0;
while (true) {
yield n++;
}
}
/**
* Creates a new function that counts infinitely (to Number.MAX_SAFE_INTEGER really)
* starting from 0.
*/
export function createCountFunc(): () => number {
const gen = count();
return (): number => gen.next().value;
}