Created
April 30, 2011 08:30
-
-
Save hakobe/949533 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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