Memoization
Writing high performance programs is usually a mixture of using good algorithms and the smart usage of computer processing power. Caching is one mechanism that can help us, especially when a method takes time to calculate or it's called a lot of times in our application.
Note
Memoization is a mechanism of recording a function result based on its arguments in order to reduce computation in consecutive calls.
Along with saving CPU cycles, memoization can also be useful to minimize the application memory footprint by only having one instance of each result. Of course, for this entire mechanism to work, we need to have a function that always returns the same result when the same arguments are passed.
Memoization example
There are different ways to achieve memoization. Some of them use imperative programming styles and it's pretty straightforward to get to them. Here we will show an approach, which is more suitable for Scala.
Let's imagine that we will need to hash strings...