Summary
We have covered how to create a small project in Scala from scratch to packaging. We used TDD along the way so that it is guaranteed that our code was well-designed and robust. It gives us confidence when we refactor code: as long as all tests pass, we know that our code stills works. We modeled our domain with immutable data structures and processed them using pure functions that have no side effects.
We used some basic features of the language that are used in most projects, and you should now be familiar with enough building blocks to implement a wide variety of projects.
As a further exercise, you could enhance this calculator with one or many of these features:
- Create an app for the function RetCalc.nbOfMonthsSaving, which calculates how long you need to save before you can retire.
- Create a function called RetCalc.annualizedTotalReturn, which calculates the geometric average of a sequence of returns. See https://www.investopedia.com/terms/a/annualized-total-return.asp for more details.
- Create a function called monthlyPension, which calculates how much you will get in retirement if you save a given amount every month for a given number of months.
- Incorporate other streams of income. Maybe you will get a state pension after a number of years, or you might get an inheritance.
- Load a different index, such as STOXX Europe 600, MSCI World Index, and so on.
- Most people do not invest all their savings in the stock market, but wisely diversify with bonds, that is, typically 60% stocks, 40% bonds. You could add a new function in Returns.scala to calculate the returns of a mixed portfolio.
- We observed that some periods have much higher returns than others. As it is difficult to predict the future, you could run multiple simulations using different periods, and calculate a probability of success.
In the next chapter, we will improve our calculator further by handling errors in a functional way.