Search⌘ K
AI Features

Extending Functions

Explore how to extend Kotlin functions by injecting custom methods such as andThen to combine and compose functions. Understand the use of extension functions to enhance function capabilities and create fluent, expressive Kotlin code. This lesson prepares you to write more modular and reusable functional code in Kotlin.

We'll cover the following...

How to extend

Functions are objects in Kotlin, and you can inject methods into functions, like you can inject methods into classes. In Java 8, the functional interface Function<T, R> has an andThen() method to combine two functions, and we can use this to compose operations—see Functional Programming in JavaVenkat Subramaniam. Functional Programming in Java. The Pragmatic Bookshelf, Raleigh, NC, 2014. Kotlin’s Function doesn’t have that method, but we can inject an ...