Skip to content

Instantly share code, notes, and snippets.

@hakobe
Created April 30, 2011 08:30
Show Gist options
  • Save hakobe/949533 to your computer and use it in GitHub Desktop.
Save hakobe/949533 to your computer and use it in GitHub Desktop.
sys = require('sys')
crypto = require('crypto')
key = (values) ->
sha1 = crypto.createHash('sha1')
sha1.update values.join('-')
sha1.digest('hex')
memoize = (f, memo = {} ) -> (args...) -> memo[k = key(args)] or memo[k] = f args...
fib = (n) -> if n < 2 then n else fib(n-1) + fib(n-2)
fib = memoize(fib)
sys.p fib 10
sys.p fib 55 # cannot calculate without memo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment