liveBook · Manning

Chapter 10: Higher-order functions:Lambdas as parameters and return values

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

Introduces higher-order functions as a way to use lambdas in your own abstractions, beyond collection and standard-library functions.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces higher-order functions as a way to use lambdas in your own abstractions, beyond collection and standard-library functions.

Key terms: lambdas, higher-order functions

10.1 Declaring functions that return or receive other functions: Higher-order functions

Defines higher-order functions in Kotlin and identifies familiar standard-library examples.

Key terms: higher-order function, lambda, function reference, predicate function, filter

10.1.1 Function types specify the parameter types and return values of a lambda

This section explains how to write function types for lambdas and how parentheses distinguish a nullable return value from a nullable function variable.

Key terms: function type, parameter types, return type, type inference, Unit, nullable return type, nullable variable

10.1.2 Calling functions passed as arguments

This section shows how a higher-order function calls a function argument, using string filtering to illustrate the call and receiver access.

Key terms: higher-order function, function passed as an argument, filter, predicate, buildString, StringBuilder, labeled this expression, outer receiver, parameter names, function type

10.1.3 Java lambdas are automatically converted to Kotlin function types

Explains how Java lambdas work with Kotlin function types, the Unit-return requirement at that boundary, and how function types are represented by generated interfaces.

Key terms: SAM conversion, functional interface, function type, receiver object, Unit, void, FunctionN, Function0<R>, Function1<P1, R>, invoke

10.1.4 Parameters with function types can provide defaults or be nullable

This section shows how function-type parameters can provide a default lambda or accept null, using string conversion in joinToString as the example.

Key terms: function type, default value, transform lambda, joinToString, nullable function type, invoke, safe-call syntax, non-nullable return type

10.1.5 Returning functions from functions

This section shows how to return a function value and use it to select behavior based on program or UI state.

Key terms: function type, getShippingCostCalculator, lambda, member reference, getPredicate, predicate, filter, prefix, ContactListFilters

10.1.6 Making code more reusable by reducing duplication with lambdas

The section shows how passing a lambda as a condition makes site-visit analysis reusable when a simple data parameter is not flexible enough.

Key terms: function types, lambda expressions, higher-order functions, strategy pattern

10.2 Removing the overhead of lambdas with inline functions

This section explains why lambdas can add runtime overhead and how the inline modifier avoids a function call by substituting a function’s code at each call site.

Key terms: lambda expression, anonymous classes, captures, inline modifier, function call

10.2.1 Inlining means substituting a function body to each call site

Explains how inline functions substitute their bodies at call sites and when a lambda argument can be inlined with them.

Key terms: inline function, call site, lambda expression, function type

10.2.2 Restrictions on inline functions

This section explains when lambda parameters can be inlined, how to exclude individual parameters from inlining, and where inline functions can be called.

Key terms: inline parameter, Illegal usage of inline-parameter, Sequence.map, transform, noinline modifier, lambda parameters, inline functions, modules, third-party libraries, Java

10.2.3 Inlining collection operations

This section explains why inline collection operations can be efficient, while chained operations may still create intermediate collections that make sequences worthwhile for large inputs.

Key terms: inline, filter, lambda, map, intermediate collection, asSequence, sequence

10.2.4 Deciding when to declare functions as inline

This section explains when declaring a function inline is likely to help and why bytecode size matters to that decision.

Key terms: inline keyword, lambda arguments, JVM, bytecode size, call site, non-inline function

10.2.5 Using inlined lambdas for resource management with withLock, use, and useLines

This section shows how inline higher-order functions manage locks and files while keeping resource cleanup out of the operation’s code.

Key terms: resource management, try/finally, withLock, Lock, use, Closeable, inline, try-with-resources, useLines, File

10.3 Returning from lambdas: Control flow in higher-order functions

Introduces the question of how return expressions behave when lambdas replace loops, without yet explaining the answer.

10.3.1 Return statements in lambdas: returning from an enclosing function

This section explains why an unqualified return in a lambda passed to an inline function can exit the enclosing function, and why that return is not allowed for a lambda passed to a non-inline function.

Key terms: return, lambda, forEach, non-local return, inline, non-inline function

10.3.2 Returning from lambdas: Return with a label

This subsection explains how labels make returns local to a lambda and how the same labeling rules apply to a lambda receiver’s this expression.

Key terms: local return, label, return@label, forEach, lambdas with receivers, implicit receiver, labeled this expression

10.3.3 Anonymous functions: Local returns by default

This section presents anonymous functions as another way to pass code to functions, emphasizing their syntax and local return behavior.

Key terms: anonymous function, block body, expression body, return type, return expression, fun keyword, early return statements, labeled

10.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 9: Operator overloading and other conventions Chapter 11: Generics →

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.