Chapter 5. Higher Order Functions and Functional Programming

In the previous chapter, we introduced Kotlin's support for functions and the various features we can use while writing functions. In this chapter, we continue on that theme by discussing higher order functions and how we can use them to write cleaner and more expressive code.

In this chapter, we will cover:

  • Higher order functions and closures
  • Anonymous functions
  • Function references
  • Functional programming idioms
  • Custom DSLs

Higher order functions

A higher order function is simply a function that either accepts another function as a parameter, returns a function as its return value, or both.

Let's consider the first example:

 fun foo(str: String, fn: (String) -> String): Unit { val applied = fn(str) ...

Get Programming Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.