Lambda Expressions

Created By: Geoffrey Challen
/ Updated: 2022-09-30

We saw how anonymous classes could encapsulate reusable logic and allow us to build, for example, a general purpose counting method. But their syntax still left a lot to be desired! In this lesson we'll improve on that by introducing lambda expressions. Awesome! Let's start.

As a reminder, this is an advanced topic. We're introducing it because you will see lambda expressions in real Kotlin code, including on the project that we start next! We won't test you heavily on it, but a simple lambda expression question may appear on an upcoming quiz.

Warm Up

But let's warm up with a classic practice problem on software testing! This is similar to the problem you'll need to solve for this lesson's homework.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Functional Programming

So far we've introduced Kotlin as an object-oriented programming language. However, Kotlin also supports other programming styles. One powerful and interesting style of programming is known as functional programming:

In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions.

Let's examine the Wikipedia definition together and contrast it with Java's object-oriented style:

Discuss the differences between object-oriented and functional programming.

First-Class Functions

One characteristic of true functional programming languages is that functions (or methods) are first-class citizens. They can be stored in variables and passed to and returned from other functions, just like any other kind of data.

Unlike Java, Kotlin does support first-class functions. However, for this lesson we'll focus on functional programming approaches that interoperate with Java. Specifically, declaring and implementing so-called functional interfaces. We may return to functional programming in Kotlin in future lesson.

Lambda Expressions

Let's look at our first lambda expression in Kotlin.

We accomplish this by combining two things we already know—interfaces and anonymous classes—with some new Kotlin syntax. Let's see how, step by step.

But first, let's state our goal.

Functional Interfaces

Our first ingredient is called a functional interface. A functional interface is any old interface, but with one restriction: it can only provide one method. We'll see why in a minute. In Kotlin we also mark it with the fun keyword.

Other than that, there are no restrictions on what a functional interface looks like. Here's one:

Here's another:

Anonymous Classes

Next, we need a way to create something that implements a functional interface on the fly. But wait—we already know how to do that! It's called an anonymous object:

Lambda Expressions

We are so close now. Imagine that we want to save into a variable a method that increments an Int by one. Here's what it looks like given what we already know. First we need our functional interface, and then an anonymous class to implement it correctly:

But we can do better! Let's see how:

Show how to convert an anonymous class implementing a functional interface to a lambda expression. Write the return version first and then convert it to a one-liner.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Array Counting with Lambdas

To finish up, let's return to our example from last time that used anonymous classes to count arrays in different ways. We'll reimplement it using lambdas and show how much cleaner and more direct this syntax is.

Reimplement the general array counter using lambda expressions.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

More Practice

Need more practice? Head over to the practice page.