Created
August 16, 2018 22:45
-
-
Save Kadajett/73c38d737db2cf78fda2706449bf5855 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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