Skip to content

Instantly share code, notes, and snippets.

@guz-anton
Created May 26, 2020 12:38
Show Gist options
  • Save guz-anton/078a25b0f4d2f7fad1b84a6219a16e0d to your computer and use it in GitHub Desktop.
Save guz-anton/078a25b0f4d2f7fad1b84a6219a16e0d to your computer and use it in GitHub Desktop.
// 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