Skip to content

Instantly share code, notes, and snippets.

@IamManchanda
Last active November 23, 2019 21:31
Show Gist options
  • Save IamManchanda/3c43aec39ebb4526de3c081ec6ba69bf to your computer and use it in GitHub Desktop.
Save IamManchanda/3c43aec39ebb4526de3c081ec6ba69bf to your computer and use it in GitHub Desktop.
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