def memoize(f):
  """Memoization decorator for a function taking a single argument."""
  class Memoize(dict):
    def __missing__(self, key):
      rv = self[key] = f(key)
      return rv
  return Memoize().__getitem__