September 2023
Intermediate to advanced
470 pages
11h 12m
English
The exercises in this chapter will focus on lazy sequences and projections.
Here is your friendly reminder that nobody can learn functional programming by just reading code, and the best way to learn is by practicing. You can refer to the exercises folder in the book code repository for the starting point and some hints.
Our first exercise is about laziness and Kotlin sequences (Sequences). We want to sum the squares of all odd numbers between two positive numbers. This is the code in nonfunctional style using for loops:
| | fun Long.isOdd(): Boolean = |
| | this % 2 != 0L |
| | |
| | fun sumOfOddSquaresForLoop(numbers: List<Long>): Long { |
| | var tot = 0L |
| | for(i in numbers){ |
| | if (i.isOdd()) tot += i*i ... |
Read now
Unlock full access