mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Add range example
This commit is contained in:
38
website/src/playground/files/root/examples/range.ts
Normal file
38
website/src/playground/files/root/examples/range.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export default function main() {
|
||||
return [...new Range(0, 10)];
|
||||
}
|
||||
|
||||
class Range {
|
||||
start: number;
|
||||
end: number;
|
||||
|
||||
constructor(start: number, end: number) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
[Symbol.iterator]() {
|
||||
return new RangeIterator(this.start, this.end);
|
||||
}
|
||||
}
|
||||
|
||||
class RangeIterator {
|
||||
value: number;
|
||||
end: number;
|
||||
|
||||
constructor(value: number, end: number) {
|
||||
this.value = value;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
next() {
|
||||
const done = this.value >= this.end;
|
||||
const res = { value: this.value, done };
|
||||
|
||||
if (!done) {
|
||||
this.value++;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user