Created
July 11, 2016 13:51
-
-
Save anonymous/d311143ab38563811e5584346c9a2a36 to your computer and use it in GitHub Desktop.
This file contains 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
def createCached meth | |
m = Module.new do | |
@cache = {} | |
def self.x args | |
p @cache | |
unless @cache[args] | |
@cache[args] = @m.call(*args) | |
end | |
end | |
end | |
m.instance_exec(meth) { @m = meth} | |
m.method(:x) | |
end | |
def fib n | |
return n if n < 2 | |
fib(n-1)+fib(n-2) | |
end | |
x = createHashed method(:fib) | |
(0..39).map { |y| x.call y } | |
(0..39).map { |y| x.call y } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment