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

E. Appendix E: Chapter 5 Exercise & Challenge Solutions
Written by Massimo Carli

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Exercise 5.1

Kotlin provides you with first, which returns the first element of Iterable<T> for which a predicate you provide as an input evaluates to true. Remember that Iterable<T> is the abstraction of all the collections providing an Iterator<T> implementation.

public interface Iterable<out T> {
  public operator fun iterator(): Iterator<T>
}

Iterator<T> allows you to iterate over all the elements of a collection in a way that doesn’t depend on the collection implementation itself:

public interface Iterator<out T> {

  public operator fun next(): T

  public operator fun hasNext(): Boolean
}

The current first signature is:

public inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T

Kotlin doesn’t allow you to override the current extension function on Iterable<T>. So, how would you implement first on Array<T>?

The current implementation of first throws an exception if the collection is empty, so there’s no first T. How would you implement the function firstOrNull on Array<T> returning null in such a case?

Exercise 5.1 solution

As mentioned, Kotlin doesn’t allow you to override the extension function on Iterable<T> so, for this exercise, you need to implement first on Array<T>. A possible solution is:

Exercise 5.2

The command pattern is another important design pattern that defines abstractions like Command and CommandExecutor. Command abstracts every possible operation that CommandExecutor can run. In other words, a Command represents a task and you can pass a Command to a CommandExecutor to run it. How would you represent them in a functional way?

Exercise 5.2 solution

Command is basically a way to abstract the concept of a task. To represent it in a functional way, write:

Exercise 5.3

Can you implement the following Reader interface as a functional interface? How would you test it?

Exercise 5.3 solution

The TODO in the problem description’s code gives you a hint about a possible solution. One option is the following:

Exercise 5.4

Implement an extension function isEqualsPredicate on the generic type T that returns a predicate that tests if a given value is equal to the same T. The signature should be the following:

Exercise 5.4 solution

A possible solution to the initial problem is the following:

Exercise 5.5

Can you implement the same logic for implementing the example in the Imperative vs. declarative approach section using the definitions of Predicate1<T> and filterWithPredicate? Given a list of email addresses, you need to:

Exercise 5.5 solution

Using the definitions of Predicate1<T> and filterWithPredicate you have in Predicates.kt and what’s in Imperative.kt and Declarative.kt, you can write:

Challenge 5.1: Mapping is important

In the chapter, you learned how to implement different types of higher-order functions, and in the next chapter, you’ll see many more. A very important one is called map. This is a function that applies a given function fn of type (A) -> B to all the elements of a collection of items of type A, getting a collection of items of type B.

Challenge 5.1 solution

A possible implementation of map for Array<A> is the following:

Challenge 5.2: Prime number filtering

Write a higher-order function all returning a new array containing all the values in an Array<T> for which a given Predicate1<T> is true. You can find the Predicate1<T> definition in Predicates.kt.

Challenge 5.2 solution

You can implement all in many different ways. One of them is:

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.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now