Update tree shaking example

This commit is contained in:
Andrew Morris
2023-07-07 10:59:07 +10:00
parent d763e0dd13
commit 4c90c5d484
2 changed files with 25 additions and 13 deletions

View File

@@ -157,8 +157,28 @@ export default class Range<T = never> implements Iterable<T> {
return new Range(res());
}
// TODO: Negative indexes
at(n: number) {
if (n < 0) {
if (n === -1) {
return this.last();
}
let buf: T[] = [];
let len = -n;
let i = 0;
for (const x of this.iterable) {
buf[i % len] = x;
i++;
}
if (buf.length < len) {
return undefined;
}
return buf[i % len];
}
let i = 0;
for (const x of this.iterable) {
@@ -345,12 +365,11 @@ export default class Range<T = never> implements Iterable<T> {
return new Range(iter());
}
// TODO: `in` operator
if (hasKey(iter, Symbol.iterator)) {
if (Symbol.iterator in iter) {
return new Range(iter);
}
if (hasKey(iter, "next")) {
if ("next" in iter) {
return Range.fromIterator(iter);
}
@@ -388,13 +407,6 @@ export default class Range<T = never> implements Iterable<T> {
}
}
function hasKey<Obj, K extends string | symbol>(
obj: unknown,
key: K,
): obj is Obj & Record<K, unknown> {
return (obj as Record<K, unknown>)[key] !== undefined;
}
function never(x: never): never {
throw new Error(`Unexpected value: ${x}`);
}

View File

@@ -8,11 +8,11 @@
// - `factorizeAsPowers`
// - `BinaryTree`
// - `NotNullish`
// - `range`
// - `Range`
//
// These definitions are not included, because the definitions exported by this
// module do not need them. Omitting those unused definitions reduces the
// bytecode for this module from 3,593 to 413 bytes.
// bytecode for this module from 3,606 to 337 bytes.
import { factorize } from "../lib/mod.ts";