Hacker News new | ask | show | jobs
by adammacleod 5317 days 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.

2 comments

The last example also doesn't actually memoize if the result of the computation is false -- it will recompute on each call.
It will store a false in the JavaScript version, I'm not familiar with Ruby but I believe you are correct in that case.
Shouldn't that be solved with a closure in both languages?
I'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.