Memoization alternatives

The memo method that we showed previously is quite neat and easy to use, but it's limiting. We can only get memoized versions of functions with one parameter (or we have to represent multiple parameters as a tuple). However, the Scalaz library already has support for memoization using the Memo object. We can simply do the following:

val memoMd5Scalaz: String => String = Memo.immutableHashMapMemo {  md5}

The preceding code can go into our Hasher class and then we can instead call memoMd5Scalaz in our example. This would not require us to write the extra Memoizer trait and it will produce absolutely the same result as what we showed previously. Moreover, the Scalaz version gives us much more flexibility in terms of the ...

Get Scala Design Patterns - Second Edition 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.