Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Ceasar
Created April 2, 2014 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ceasar/9943217 to your computer and use it in GitHub Desktop.
Save Ceasar/9943217 to your computer and use it in GitHub Desktop.
_ = 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