Created
May 26, 2020 12:38
-
-
Save guz-anton/078a25b0f4d2f7fad1b84a6219a16e0d 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
// const freezeFn = fn => (c => x => c[x] != null ? c[x] : c[x] = fn(x))({}) | |
const freezeFn = fn => (c => {return function cached(x){if (c[x] != null) {c[x] = fn(x, cached)}; return c[x]}})({}) | |
const fibFn = (x, fn) => fn(x - 1) + fn(x - 2) | |
const fibArgs = (x, fn) => +!parseInt(x) || +(x == 0) || +(x == 1) || fibFn(parseInt(x), fn) | |
const fibonacci = freezeFn(fibArgs) | |
const facFn = (x, fn) => x * fn(x - 1) | |
const facArgs = (x, fn) => +!parseInt(x) || +(x == 0) || +(x == 1) || facFn(parseInt(x), fn) | |
const factorial = freezeFn(facArgs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment