Skip to content

Instantly share code, notes, and snippets.

@denniskigen
Created August 24, 2020 10:51
Show Gist options
  • Save denniskigen/dcb7b206e5d25bc895601765b160f5ff to your computer and use it in GitHub Desktop.
Save denniskigen/dcb7b206e5d25bc895601765b160f5ff 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