- 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 _.
- EJB 3.1從入門到精通
- CorelDRAW X6 中文版圖形設計實戰從入門到精通
- Django 2 by Example
- 新一代物聯網架構技術:分層算力網絡
- NB-IoT物聯網技術解析與案例詳解
- 射頻通信系統
- Learning Swift(Second Edition)
- C/C++串口通信:典型應用實例編程實踐
- CCNP TSHOOT(642-832)認證考試指南
- WordPress Web Application Development
- 搶占下一個智能風口:移動物聯網
- 光纖通信系統與網絡(修訂版)
- INSTANT LinkedIn Customization How-to
- 圖解物聯網
- Intelligent Mobile Projects with TensorFlow