3 ms·
As the siblings have said, it's an extremely situational decision whether to memoize a pure function, and only a tiny number of pure functions benefit from it.
by OskarS 7y ago
As the siblings have said, it's an extremely situational decision whether to memoize a pure function, and only a tiny number of pure functions benefit from it. There's no way to do it automatically, it has to be programmer choice.
For instance, take the function that concatenates two strings into a third string. That's a pure function, but it would be madness to memoize it. It would mean saving every single string concatenation your program ever does in memory, for no real reason at all.
Or, you know: addition. Taking two numbers and returning the result of adding them together is a pure function. Should you memoize that? Literally every single addition you ever do would have to be saved, and every addition would have to do a hash-table look-up.