Skip to content

Instantly share code, notes, and snippets.

@mmrobins
Created June 5, 2013 02:52
Show Gist options
  • Save mmrobins/5711289 to your computer and use it in GitHub Desktop.
Save mmrobins/5711289 to your computer and use it in GitHub Desktop.
Markus' Ruby hangman to implement an LRU function
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