23 lines
665 B
TypeScript
23 lines
665 B
TypeScript
export const to_int = (n: string) => ((parsed) => isNaN(parsed) ? 0 : parsed)(parseInt(n));
|
|
|
|
export function range(from: number, to: number) {
|
|
function* gen() {
|
|
while (from < to) yield from++;
|
|
}
|
|
return iterable_to_arrayable(gen());
|
|
}
|
|
|
|
export function enumerate<T>(iterable: Iterable<T>) {
|
|
function* gen() {
|
|
let index = 0;
|
|
for (const item of iterable) yield [index++, item] as const;
|
|
}
|
|
return iterable_to_arrayable(gen());
|
|
}
|
|
|
|
function iterable_to_arrayable<T>(iterator: Iterable<T>) {
|
|
// deno-lint-ignore no-explicit-any
|
|
const anyfied = iterator as any;
|
|
anyfied.arr = () => Array.from(iterator);
|
|
return anyfied as Iterable<T> & { arr: () => T[] };
|
|
}
|