Skip to content

Instantly share code, notes, and snippets.

@tmikeschu
Last active June 24, 2018 14:05
Show Gist options
  • Save tmikeschu/ce7a02b733182061232cb9c486a3ff7f to your computer and use it in GitHub Desktop.
Save tmikeschu/ce7a02b733182061232cb9c486a3ff7f to your computer and use it in GitHub Desktop.
Give that recursive function everything it needs!
// feel free to paste this in your JS console!
const slowfib = x => {
if (x === 0) return null
if (x < 5) return [0,1,1,2][x -1]
return slowfib(x - 2) + slowfib(x - 1)
}
const F = () => false
const add = (x, y) => !y ? z => x + z : x + y
const dec = add(-1)
const fib = x => {
const fibLookup = {
0: () => null,
1: (a, _) => a,
2: (_, b) => b,
}
const _fib = (y, a, b) =>
fibLookup[y] === undefined
? _fib(dex(y), b, add(a, b))
: fibLookup[y](a, b)
return _fib(x, 0, 1)
}
console.time('slowfib')
slowfib(45)
console.timeEnd('slowfib')
console.time('fib')
fib(45)
console.timeEnd('fib')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment