human
I am working on the file "20150223-cohpy-memoization.ipynb".
The first task is:
Taking out the diagnostic counting code made it faster yet. 
The caching code makes the fib() function ugly and I don't 
like needing to remember to initialize the cache, 
so let's separate the caching into a separate function. 
Note that memoize() doesn't know anything about the fib() function, 
except that it takes a single argument.
memoize() can be applied to many single argument functions.
Can you write Python code for it?
assistant
#Here is the Python code you requested.

def memoize(f):
    results = {}
    def helper(x):
        if x not in results:
            results[x] = f(x)
        return results[x]
    return helper

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

fib = memoize(fib)

def foo(n):
    def fib(n):
        if n == 0:
            return 0
        elif n == 1:
            return 1
        else:
            return fib(n-1) + fib(n-2)
    fib = memoize(fib)
    %timeit -n 1 fib(n)

for i in range(20):
    foo(n)