I'm a bad cacher: Sometimes, when no one is watching, I'll cache results without including the full context like so:
f[x_]:=f[x]=x+a;
a=2; f[1];
DownValues[f]
Out[2]= {HoldPattern[f[1]]:>3,HoldPattern[f[x_]]:>(f[x]=x+a)}
This leads to horribly subtle bugs, and, more importantly, to the need for clearing the cache when I change the context. One way of clearing the cache is to completely Clear
the symbol and repeat the definitions, but this is not really a solution.
What I would really like is a method for clearing all pattern-free DownValues associated with a symbol.
For clarity, I'll include my present solution as an answer, but if fails on two counts
- It only clears DownValues with all-numeric arguments
- For aesthetical reasons, I'd like to avoid using
Block
to grab the DownValues.
Any ideas on how to improve ClearCache
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…