Memoization
I invite you to work with me on a small math problem. Grab a paper and pencil. All set? OK, here we go. What’s 321 + 174? Without a calculator on hand it’s not something I can answer spontaneously, and if you need a moment to find the answer, that’s perfectly normal.
Did you find it? If you wrote down 495 you got it without any errors. Good job.
Now, one more question. What’s 321 + 174? Now, you answered that in a snap, didn’t you? How did you get so fast so quickly?
It’s ok to admit, you looked up the result from the previous computation. That’s pretty smart; you wouldn’t redundantly compute the expression that you evaluated only a moment ago. That’s memoization when done in code—we don’t want our programs to recompute functions ...
Get Programming Kotlin now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.