Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
_ = 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
You can’t perform that action at this time.