liveBook · Manning

Chapter 5: Programming with lambdas

What you will learn in chapter 5 of Kotlin in Action, 2E

The chapter introduces lambdas as passable chunks of code that help make Kotlin code reusable and expressive.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces lambdas as passable chunks of code that help make Kotlin code reusable and expressive.

Key terms: lambda expressions, lambdas, library functions

5.1 Lambda expressions and member references

This introduction notes the anticipation surrounding Java 8 lambdas and previews a discussion of their usefulness and Kotlin syntax.

5.1.1 Introduction to lambdas: Blocks of code as values

Introduces lambdas as a concise way to pass behavior as a value, using a button-click listener to contrast them with an object implementation.

Key terms: lambda expressions, functions as values, OnClickListener, onClick, first-class functions, immutability, no side effects, pure

5.1.2 Lambdas and collections

This section shows how a lambda-based collection function can replace a manual search for the oldest person.

Key terms: maxByOrNull, lambda, selector, it, member reference, collections, lambdas, standard library, loop-based counterpart

5.1.3 Syntax for lambda expressions

This section explains Kotlin lambda syntax and how to make lambda expressions concise while keeping them readable.

Key terms: lambda expression, parameters, body, last expression, last argument, named argument, parentheses, lambda parameter type, type inference, it

5.1.4 Accessing variables in scope

This section explains how Kotlin lambdas access and modify surrounding variables, how captured variables are retained, and why execution timing matters.

Key terms: lambda, captured, local variables, parameters, val, var, Ref class, event handler, onClick handler, local variable

5.1.5 Member references

This section shows how Kotlin member references turn existing functions, properties, and constructors into values that can be used in place of lambdas.

Key terms: member reference, :: operator, function value, top-level function, extension function, ::salute, constructor reference

5.1.6 Bound callable references

This subsection explains how a bound callable reference captures a member of a specific object, unlike a regular member reference that takes an object as an argument.

Key terms: bound callable references, regular member reference, seb::age

5.2 Using Java functional interfaces: Single abstract methods

This section explains why Kotlin can pass a lambda to a Java method that expects a functional interface, using a button click listener as the example.

Key terms: functional interface, single abstract method (SAM) interface, OnClickListener, onClick, lambda

5.2.1 Passing a lambda as a parameter to a Java method

This subsection explains how Kotlin passes lambdas to Java methods expecting functional interfaces, when the resulting object can be reused, and why this account differs for inline Kotlin functions.

Key terms: lambda, functional interface, Runnable, single abstract method, run, anonymous object, captured variables, anonymous class instance, Java methods, functional interfaces

5.2.2 SAM constructors: Explicit conversion of lambdas to functional interfaces

This section explains how SAM constructors explicitly convert lambdas to functional-interface instances and when an anonymous object is needed instead.

Key terms: SAM constructor, functional interface, single abstract method, SAM conversion, lambda, this, anonymous object, event listener

5.3 Defining SAM interfaces in Kotlin: fun interfaces

This section explains when to define a Kotlin fun interface, how to supply its implementation with a lambda, and why it can make Java call sites cleaner.

Key terms: fun interface, functional interface, function type, abstract method, non-abstract method, SAM constructor, lambda, IntCondition, Java call site, Kotlin function type

5.4 Lambdas with receivers: with, apply, and also

Introduces lambdas with receivers as a Kotlin feature that lets a lambda call another object’s methods without additional qualifiers.

Key terms: lambdas with receivers, qualifiers

5.4.1 Performing multiple operations on the same object: with

This section shows how `with` lets a lambda perform several operations on one object without repeatedly naming it, and explains what the call returns.

Key terms: with, lambda with a receiver, receiver, this, extension function, last expression, explicit label

5.4.2 Initializing and configuring objects: The apply function

Explains how apply configures an object through a receiver lambda and returns that object, then introduces specialized builder functions that use the same pattern.

Key terms: apply, receiver, lambda with a receiver, with, buildString, StringBuilder, buildList, buildSet, buildMap

5.4.3 Performing additional actions with an object: also

This section explains how also performs an additional action with an object while returning that object for continued use.

Key terms: also, apply, it, receiver object

5.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Classes, objects, and interfaces Chapter 6: Working with collections and sequences →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.