Created
April 2, 2014 21:07
-
-
Save Ceasar/9943217 to your computer and use it in GitHub Desktop.
This file contains 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
_ = require "lodash" | |
memoize = (f) -> | |
cache = {} | |
memoizer = -> | |
args = Array.prototype.slice.call(arguments) | |
if _.has(cache, args) then cache[args] else cache[args] = f.apply this, args | |
return memoizer | |
fib = (n) -> | |
if n in [0, 1] then n else fib(n - 1) + fib(n - 2) | |
fib = memoize(fib) | |
console.log (fib 40) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment