Skip to content

Instantly share code, notes, and snippets.

@Kadajett
Created August 16, 2018 22:45
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 Kadajett/73c38d737db2cf78fda2706449bf5855 to your computer and use it in GitHub Desktop.
Save Kadajett/73c38d737db2cf78fda2706449bf5855 to your computer and use it in GitHub Desktop.
/**
* This is basic memoization. I am going to play around with it and come up with a few solutions!
* Probably going to go back and rewrite this. Its kinda ugly.
*/
class Fib {
constructor() {
this.cache = {
"10": [0,1,1,2,3,4,8,13, 21, 34]
}
}
*fibGen(n = null, current = 0, next = 1) {
const inifinite = !n && n !== 0;
let firstN = n.toString();
this.cache[firstN] = this.cache[firstN] || [];
// ugly!
if(this.cache[firstN] && this.cache[firstN].length === parseInt(firstN)) {
// Due to the nature of Yield, cached is being called before it prints out the array.
// But, its only being printed once, so you know its working!
console.log("Cached!");
yield* this.cache[firstN];
return;
}
while (inifinite || n--) {
this.cache[firstN].push(current);
yield current;
[current, next] = [next, current + next];
}
}
}
module.exports = Fib;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment