Last active
November 23, 2019 21:31
-
-
Save IamManchanda/3c43aec39ebb4526de3c081ec6ba69bf to your computer and use it in GitHub Desktop.
Fib function as showed on https://twitter.com/HarmanManchanda/status/1198347925039939584
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
const memoize = (fn) => { | |
const cache = {}; | |
return (...args) => { | |
if (cache[args]) { return cache[args]; } | |
cache[args] = fn.apply(this, args); | |
return cache[args]; | |
}; | |
}; | |
const range = (count) => Array.from({ length: count }, (v, i) => i + 1); | |
let fibGen = (n) => (n < 2) ? n : fibGen(n - 1) + fibGen(n - 2); | |
fibGen = memoize(fibGen); | |
const fib = (items) => { | |
let all = [0]; | |
if (items > 0) for (i of range(items)) all.push(fibGen(i)); | |
return all; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment