add range class

This commit is contained in:
JOLIMAITRE Matthieu 2024-06-15 01:28:46 +02:00
parent 2e7cfb9deb
commit a84450fc23
3 changed files with 29 additions and 5 deletions

View file

@ -3,8 +3,9 @@ export type { Arr, ClassOf, Constructible, Function, InstanceOf, KeyOfType, Tail
// functions // functions
export { all, enumerate, filter, filter_map, it, Iter, map, zip } from "./lib/iter.ts"; export { all, enumerate, filter, filter_map, it, Iter, map, zip } from "./lib/iter.ts";
export { log_from, next, range, split_promise, wait } from "./lib/utils.ts"; export { log_from, next, split_promise, wait } from "./lib/utils.ts";
// Structures // Structures
export { Chain } from "./lib/Chain.ts"; export { Chain } from "./lib/Chain.ts";
export { Channel } from "./lib/Channel.ts"; export { Channel } from "./lib/Channel.ts";
export { Range, range } from "./lib/Range.ts";

27
src/lib/Range.ts Normal file
View file

@ -0,0 +1,27 @@
export class Range {
from;
to;
constructor(from: number, to: number) {
this.from = from;
this.to = to;
}
*[Symbol.iterator](): Generator<number, void, void> {
let index = this.from;
while (index < this.to) yield index++;
}
includes(n: number) {
return (n > this.from) && (n < this.to);
}
}
export function range(from: number, to: number) {
return new Range(from, to);
}
Deno.test("range", async () => {
const { assertEquals } = await import("https://deno.land/std@0.224.0/assert/assert_equals.ts");
assertEquals([...range(0, 4)], [0, 1, 2, 3]);
});

View file

@ -18,10 +18,6 @@ export function next<T>(iterator: Iterator<T>) {
else return result as T; else return result as T;
} }
export function* range(from: number, to: number) {
while (from < to) yield from++;
}
/** /**
* usage: * usage:
* ```ts * ```ts