Skip to content

Instantly share code, notes, and snippets.

@denniskigen
Created August 24, 2020 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denniskigen/273757dc4bd057474ada5be0d1a9eb2d to your computer and use it in GitHub Desktop.
Save denniskigen/273757dc4bd057474ada5be0d1a9eb2d to your computer and use it in GitHub Desktop.
class Fibonacci implements IterableIterator<number> {
protected f1 = 0;
protected f2 = 1;
constructor(protected maxValue?: number) {}
public next(): IteratorResult<number> {
if (this.maxValue != null && current >= this.maxValue) {
return {
done: true,
value: null
}
}
return {
done: false,
value: null
}
}
[Symbol.iterator](): IterableIterator<number> {
return this;
}
}
let fibMax1000 = new Fibonacci(1000);
Array.from(fibMax1000) // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment