Refactoring monthlyRate

In Chapter 2Developing a Retirement Calculator, we implemented a Returns.monthlyRate(returns: Returns, month: Int): Double function that returned the monthly return rate for a given month. When we called it with a month exceeding the size of an instance of VariableReturns, we rolled over to the first month using a modulo operation.

This was not completely satisfying, as it could compute unrealistic simulations. Say that your VariableReturns instance contains data for 1950 to 2017. When you ask the monthly returns for 2018, monthlyRate would give you the returns that we had in 1950. The economic outlook of the fifties was very different compared to the current one, and it is unlikely that the returns in 2018 will ...

Get Scala Programming Projects now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.