Skip to content

Instantly share code, notes, and snippets.

@satyr
Forked from hakobe/fib.coffee
Created April 30, 2011 14:04
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 satyr/949694 to your computer and use it in GitHub Desktop.
Save satyr/949694 to your computer and use it in GitHub Desktop.
crypto = require 'crypto'
key = (values) ->
crypto.createHash('sha1').update(values.join '-').digest 'hex'
memoize = (f, memo = {}) ->
-> memo[key [this, arguments...]] ||= f.apply this, arguments
fib = memoize (n) -> if n < 2 then n else fib(n-1) + fib(n-2)
console.log fib 10
console.log fib 55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment