- Scala Programming Projects
- Mikael Valot Nicolas Jorand
- 218字
- 2021-07-23 16:25:19
Refactoring the production code
In the TDD approach, it is common to refactor the code once we have passing tests. If our test coverage is good, we should not have any fear of changing the code, because any mishap should be flagged by a failing test. This is known as a Red-Green-Refactor cycle.
Change the body of futureCapital with the following code:
def futureCapital(interestRate: Double, nbOfMonths: Int, netIncome: Int, currentExpenses: Int, initialCapital: Double): Double = {
val monthlySavings = netIncome - currentExpenses
(0 until nbOfMonths).foldLeft(initialCapital)(
(accumulated, _) => accumulated * (1 + interestRate) +
monthlySavings)
}
Here, we have inlined the nextCapital function in the foldLeft call. In Scala, we can define an anonymous function using the syntax:
(param1, param2, ..., paramN) => function body.
We saw earlier that the month parameter in nextCapital was not used. In an anonymous function, it is a good practice to name any unused parameter with _. A parameter named _ cannot be used in the function body. If you try to replace the _ character with a name, IntelliJ will underline it. If you hover the mouse over it, you will see a popup stating Declaration is never used. You can then hit Alt + Enter and choose to remove the unused element to automatically change it back to _.
- 智慧城市:大數據、互聯網時代的城市治理(第4版)
- Aptana Studio Beginner's Guide
- 物聯網智慧安監技術
- Twilio Cookbook
- SEO 20日
- 網絡創新指數研究
- Learning Karaf Cellar
- Go Web Scraping Quick Start Guide
- 邁向自智網絡時代:IP自動駕駛網絡
- 網管工具使用與技巧大全
- Python API Development Fundamentals
- Enterprise ApplicationDevelopment with Ext JSand Spring
- 物聯網,So Easy!
- Cisco無線局域網配置基礎
- 區塊鏈技術與應用:打造分布式商業新生態