July 2023
Intermediate to advanced
276 pages
6h 55m
English
Existing codebases are littered with for loops. We use them to iterate over collections of data but also over a range of indexes. To illustrate how we can refactor the traditional imperative for loop into functional style code, we’ll look at using a for loop to compute the factorial of numbers in a range. But, of course, we’ll start with a test to provide the safety net we just discussed.
| | public class FactorialTest { |
| | Factorial factorial; |
| | |
| | @BeforeEach |
| | public void init() { |
| | factorial = new Factorial(); |
| | } |
| | |
| | @Test |
| | public void computeFactorial() { |
| | assertAll( |
| | () -> assertEquals(BigInteger.ONE, factorial.compute(1)), |
| | () -> assertEquals(BigInteger.TWO, ... |
Read now
Unlock full access