3 ms·
I'd be careful with the last Memoize example, if current_date changes then the JavaScript function at least will return the old value still. Memoize only works
by adammacleod 15y ago
I'd be careful with the last Memoize example, if current_date changes then the JavaScript function at least will return the old value still.
Memoize only works if your function is one to one (one set of inputs gives the same outputs), and you cache the result of the function for each value of the input.
- evmar 15y agoThe last example also doesn't actually memoize if the result of the computation is false -- it will recompute on each call.
- adammacleod 15y agoIt will store a false in the JavaScript version, I'm not familiar with Ruby but I believe you are correct in that case.
- gizzlon 15y agoShouldn't that be solved with a closure in both languages?
- qntm 15y agoI'm not sure there's anything there to be solved. If a "function" can return two different answers despite being provided with the exact same input parameters, you simply should not be attempting to memoize it.