Steven D'Aprano <[email protected]> writes:
> [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data]
def memoize(f):
cache = {}
def m(x):
if x in cache:
return cache[x]
a = f(x)
cache[x] = a
return a
return m
ec = memoize(expensive_calculation)
... [(ec(x), ec(x) + 1) for x in data]
Or can write:
@memoize
def expensive_calculation(x): ....
Note the Haskell version of your listcomp would be:
[(e, e+1) | x <- data_, let e = expensive_calculation x]
Maybe Python could get some version of that. I've wanted it more than
once. (I used "data_" because data is a Haskell keyword).
--
https://mail.python.org/mailman/listinfo/python-list