#!/usr/bin/env python
def memoize(function, cached={}):
def wrapper(x, y):
if (x, y) not in cached:
cached[(x, y)] = function(x, y)
return cached[(x, y)]
return wrapper
class Memoization(object):
def __init__(self, function):
self.function = function
self.cached = {}
def __call__(self, x, y):
if (x, y) not in self.cached:
self.cached[(x, y)] = self.function(x, y)
return self.cached[(x, y)]
@memoize
def add(a, b):
print "%s + %s" % (a, b)
return a + b
@Memoization
def rem(c, d):
print "%s - %s" % (c, d)
return c - d
print add(1, 3)
print add(1, 4)
print add(1, 3)
print rem(1, 3)
print rem(1, 4)
print rem(1, 3)