Proposal: Relax rules for 'pure'

bearophile bearophileHUGS at lycos.com
Mon Oct 25 09:44:08 PDT 2010


Andrei:

> Speaking of which - memoization should be implementable safely and 
> efficiently in a library. I'm thinking of something like this:
> 
> alias memoize!sin msin;
> ...
> double x = msin(angle);

Good.


> It does get tricky though. For example, for doubles you'd need to do 
> some interpolation and there are several ways of interpolating. For 
> other types you need to make sure there's no undue aliasing, etc.

Not good. Please keep things simple. If the programmer wants to use floats, then it's not the job of the memoizing function to define tolerances, etc.

If you want to add other features to a memoizing function, here are two useful features:
- An optional max number of items stored in the cache, using a LRU strategy (Python 3.2 standard library has this).
- An optional max number of bytes used by the cache (this is not present in Python std lib, but I have found this useful).

The first feature (max number of items using LRU) may be done wrapping the associative array keys inside a linked list.

Bye,
bearophile


More information about the Digitalmars-d mailing list