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

H. Appendix H: Chapter 8 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 8.1

In this chapter, you implemented the generic curry function that basically maps a function of type (A, B) -> C in a function of type (A) -> (B) -> C. Can you now implement the uncurry function, which does the inverse? It’s a function that maps a function of type (A) -> (B) -> C in a function of type (A, B) -> C.

Exercise 8.1 solution

The implementation of the uncurry function is:

Exercise 8.2

Implement a higher-order function flip that maps a function of type (A, B) -> C in the function (B, A) -> C, flipping the order of the input parameters.

Exercise 8.2 solution

The flip function is very interesting and useful. Given you already have curry and uncurry, you can implement flip like this:

Exercise 8.3

The curry function maps a function of type Fun2<A, B, C> to a function of type (A) -> (B) -> C. How would you define an overload of curry for functions of three, four, five or, in general, n parameters?

Exercise 8.3 solution

To make the code easier to read, start by writing a typealias for each function type with a specified number of parameters, from 3 until 5 like this:

Exercise 8.4

How would you apply the previous pattern for Array<T>? Basically, you need a way to compose functions of type:

Exercise 8.4 solution

First, you need to understand what composing functions of type ToArray<A, B> means. The first is a function receiving an input value of type A and returning an Array<B>. The second receives an input of type B and returns an Array<C>.

Challenge 1: Callable stuff

In the chapter, you learned how to implement the compose function in different scenarios following a common pattern. Consider, now, the following function type:

Challenge 1 solution

Following the same pattern you learned in the chapter, you can implement compose like this:

Challenge 2: Parameters or not parameters?

Suppose you have the following functions:

Challenge 2 solution

The solution to this challenge is very simple. You just need to define the following functions:

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