conditional computation

robert no-spam at no-spam-no-spam.com
Thu Oct 26 22:22:27 CEST 2006


Mike Kent wrote:
> robert wrote:
>> I want to use a computation cache scheme like
>>
>>
>> 	o = CACHECOMPUTE  complex-key-expr  expensive-calc-expr
>>
>>
>> frequently and elegantly without writing complex-key-expr or expensive-calc-expr twice.
>> So its ugly:
>>
>> 	_=complex-key-expr; o=cache.get(_) or cache.setdefault(_,expensive-calc-expr)
>>
>> Any ideas?
>>
>> -robert
> 
> Your question is a bit terse, so my answer might not be spot on for it,
> but it sounds like you want what is typically called 'memoization',
> whereby a function caches its expensive-to-calculate return values,
> where the cache is keyed by the function arguments.
> 
> See:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52201
> and various other implimentations in the Cookbook for details.
> 

thanks, yes, but the challenge was to hook expressions for memoizing ad hoc. See above solution.



More information about the Python-list mailing list