Last active
August 23, 2016 17:27
-
-
Save marcmartino/722840598cb1ff5fcd5aba7dd344a266 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(num) => num <= 1 ? num : fibby(...)
I'd throw a ternary on line 22