pfes
This commit is contained in:
parent
a94776695a
commit
8b3bb9c382
6 changed files with 119 additions and 80 deletions
23
pfes/sort/utils.ts
Normal file
23
pfes/sort/utils.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
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[] };
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue