September 2018
Intermediate to advanced
398 pages
9h 43m
English
In functional programming, we avoid mutating variables. In an imperative language, you would typically implement nbOfMonthsSaving by using a while loop. It is also possible to do so in Scala, but it is a better practice to only use immutable variables. One way of solving this problem is to use recursion:
def nbOfMonthsSaving(interestRate: Double, nbOfMonthsInRetirement: Int, netIncome: Int, currentExpenses: Int, initialCapital: Double): Int = { def loop(months: Int): Int = { val (capitalAtRetirement, capitalAfterDeath) = simulatePlan( interestRate = interestRate, nbOfMonthsSaving = months, nbOfMonthsInRetirement = nbOfMonthsInRetirement, netIncome = netIncome, currentExpenses = currentExpenses, initialCapital = ...
Read now
Unlock full access