Skip to content

Instantly share code, notes, and snippets.

@marcmartino
Last active August 23, 2016 17:27
Show Gist options
  • Save marcmartino/722840598cb1ff5fcd5aba7dd344a266 to your computer and use it in GitHub Desktop.
Save marcmartino/722840598cb1ff5fcd5aba7dd344a266 to your computer and use it in GitHub Desktop.
function genSeqUntil(valFunc, limitPredicate, seq = []) {
var nextVal = valFunc(seq.length);
if (limitPredicate(seq.length, nextVal)) {
seq[seq.length] = nextVal;
return genSeqUntil(valFunc, limitPredicate, seq);
}
return seq;
}
const memoize = (fn) => (function () {
var cache = {};
return function () {
var strArgs = JSON.stringify(arguments);
if (!(strArgs in cache)) {
cache[strArgs] = fn.apply(undefined, arguments);
}
return cache[strArgs];
}
}()),
fibby = memoize((num) =>
num <= 1 ? num : fibby(num - 1) + fibby(num - 2));
genSeqUntil(fibby, (i, v) => v < 4000000)
.filter((num) => num % 2 === 0)
.reduce((accum, v) => accum + v, 0);
@nater1067
Copy link

(num) => num <= 1 ? num : fibby(...)
I'd throw a ternary on line 22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment