Created
June 5, 2013 02:52
-
-
Save mmrobins/5711289 to your computer and use it in GitHub Desktop.
Markus' Ruby hangman to implement an LRU function
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 LRU(n, &b) | |
h={} | |
->(*k) do | |
h[k] = h.delete(k) do | |
puts "cache miss" | |
if h.length >= n | |
puts "clearing cache past #{n}" | |
h.shift | |
end | |
b[*k] | |
end | |
end | |
end | |
def bar(a,b,c) | |
@bar ||= LRU(3) { |a,b,c| a * b * c } | |
@bar[a,b,c] | |
end | |
puts bar(1,2,3) | |
puts bar(2,2,3) | |
puts bar(2,2,3) | |
puts bar(2,2,3) | |
puts bar(2,2,3) | |
puts bar(3,2,3) | |
puts bar(4,2,3) | |
puts bar(5,2,3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment