Chapters

Hide chapters

Functional Programming in Kotlin by Tutorials

First Edition · Android 12 · Kotlin 1.6 · IntelliJ IDEA 2022

Section I: Functional Programming Fundamentals

Section 1: 8 chapters
Show chapters Hide chapters

Appendix

Section 4: 13 chapters
Show chapters Hide chapters

A. Appendix A: Chapter 1 Exercise Solutions
Written by Massimo Carli

Exercise 1.1

Implement the function sumInRange that sums the values in a List<String> within a given interval. The signature is:

 fun sumInRange(input: List<String>, range: IntRange): Int

Exercise 1.1 solution

A possible solution is the following:

fun sumInRange(input: List<String>, range: IntRange): Int = // 1
  input
    .filter(::isValidNumber) // 2
    .map(String::toInt) // 3
    .filter { it in range } // 4
    .sum() // 5

In this code, you:

  1. Define the sumInRange as requested.
  2. Use filter with isValidNumber, which you defined in the chapter.
  3. Invoke map, passing String::toInt to covert the String to Int.
  4. Use filter again, passing a lambda that checks whether the value is in the range you pass in as input.
  5. Invoke sum.

To test the previous code, run:

fun main() {
  println(sumInRange(listOf("1", "10", "a", "7", "ad2", "3"), 1..5))
}

Getting:

4

This is the sum of the values in List<String> that are valid Ints and in the range 1..5.

Exercise 1.2

Implement chrono, which accepts a function of type () -> Unit as input and returns the time spent to run it. The signature is:

fun chrono(fn: () -> Unit): Long

Exercise 1.2 solution

A possible implementation for chrono is the following:

fun chrono(fn: () -> Unit): Long { // 1
  val start = System.currentTimeMillis() // 2
  fn() // 3
  return System.currentTimeMillis() - start // 4
}

In this code, you:

  1. Define chrono as a function accepting a lambda as an input parameter and returning a Long.
  2. Save the current time in milliseconds in the start variable.
  3. Invoke the function fn you get as an input parameter.
  4. Return the difference between the current time and the one in start.

One way to test this is:

fun main() {
  val waitOneSec = { Thread.sleep(1000) } // 1
  println(chrono(waitOneSec)) // 2
}

Here, you:

  1. Define the waitOneSec lambda that waits at least 1000 milliseconds.
  2. Invoke chrono, passing waitOneSec and printing the result.

When you run that code, you get something like:

1005

Note: Even if not strictly related to functional programming, it’s useful to mention why the result isn’t exactly 1000. You might even get a different result every time you run the previous main. This is due to the sleep function of the Thread class. It asks the current thread to go to the Wait state for 1000 milliseconds. After the 1000 milliseconds, the task may or may not be the next to proceed. You only know that the thread will move from the Wait state to the Runnable state. When the thread will actually continue depends on the scheduler responsible for moving a thread from the Runnable state to the Running one. This is why you can never get a value less than 1000.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.