Re:Basic Patterns for Everyday Programming
by evmar on Wednesday November 23, 2011 @01:36AM
(#3269064)
The last example also doesn't actually memoize if the result of the computation is false -- it will recompute on each call.
Re:Basic Patterns for Everyday Programming
by adammacleod on Wednesday November 23, 2011 @01:38AM
(#3269069)
It will store a false in the JavaScript version, I'm not familiar with Ruby but I believe you are correct in that case.
Re:Basic Patterns for Everyday Programming
by gizzlon on Wednesday November 23, 2011 @03:15AM
(#3269255)
Shouldn't that be solved with a closure in both languages?
Re:Basic Patterns for Everyday Programming
by qntm on Wednesday November 23, 2011 @06:33AM
(#3269685)
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.
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.