Hide chapters

Functional Programming in Kotlin by Tutorials

1 min

Section I: Functional Programming Fundamentals

Section 1: 8 chapters
Show chapters Hide chapters


Section 4: 13 chapters
Show chapters Hide chapters

L. Appendix L: Chapter 13 Exercise Solutions
Written by Massimo Carli

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

Exercise 13.1

How would you make the Optional<T> data type you created in Chapter 9, “Data Types”, a monad?

Exercise 13.1 solution

In Chapter 9, “Data Types” you implemented the Optional<T> type like this:

sealed class Optional<out T> {
  companion object {
    fun <T> lift(value: T): Optional<T> = Some(value)

    fun <T> empty(): Optional<T> = None

object None : Optional<Nothing>()
data class Some<T>(val value: T) : Optional<T>()
fun <A, B> Optional<A>.map(fn: Fun<A, B>): Optional<B> =
  when (this) {
    is Some<A> -> Some(fn(this.value))
    is None -> None
fun <T> Optional<Optional<T>>.optionalFlatten(): Optional<T> = when (this) {
  is Some<Optional<T>> -> when (this.value) {
    is Some<T> -> Optional.lift<T>(this.value.value)
    is None -> Optional.empty()
  is None -> Optional.empty()
infix fun <B, C> Optional<B>.optionalBind(
  g: Fun<B, Optional<C>>
): Optional<C> =
infix fun <A, B, C> Fun<A, Optional<B>>.optionalFish(
  g: Fun<B, Optional<C>>
): Fun<A, Optional<C>> = { a: A ->

Exercise 13.2

What’s the relation between the fish operator, >=>, and flatMap? Can you express the latter in terms of the former for Optional<T>?

Exercise 13.2 solution

The >=> operator for Optional<T> has type:

((A) -> Optional<B>, (B) -> Optional<C>) -> (A) -> Optional<C>
fun <A> Optional<A>.lift(value: A): Optional<A> =
  Optional.lift(value) // 1

fun <A, B> Optional<A>.flatMap(fn: Fun<A, Optional<B>>): Optional<B> =
  map(::lift optionalFish fn).optionalFlatten() // 2
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.
© 2023 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now