Programming with Lambdas
ver. 1.0.0
Learn idiomatic Kotlin lambdas, collection transformations (eager and lazy), Java SAM interop, and lambdas with receivers
This unit teaches idiomatic Kotlin functional programming: how to write and shorten lambda expressions, use member references, transform collections with functions like
filter,map,flatMap, and the query helpers (all,any,count,find,groupBy), avoid intermediate collections withSequenceandasSequence(), interoperate with Java functional interfaces using SAM conversion and SAM constructors, and write lambdas with receivers usingwithandapply.
By the end of this unit you will be able to write Kotlin lambdas idiomatically and apply the syntactic conventions that progressively shorten them (full brace-and-arrow form, trailing lambda placement, omitting parentheses, type inference, and the implicit it parameter for single-argument lambdas). You will know when a simple forwarding lambda can be replaced by a member reference (Type::member, including properties, member functions, top-level functions, and constructors) to make code clearer and shorter.
You will use the core collection APIs to transform and query data without mutating the original collections: filter to keep elements, map to transform them, flatMap to map-and-flatten, and helper queries such as all, any, count, and find. You will also use groupBy to turn a list into a map keyed by some selector.
You will understand the cost of chained eager collection operations (each step builds an intermediate collection) and when that cost matters. When appropriate, you will convert collections to lazy Sequences with asSequence() or create sequences from generators or iterative producers so operations are fused: elements are processed one-by-one through the whole chain and no intermediate collections are built until a terminal operation runs.
You will interoperate with Java’s single-abstract-method (functional) interfaces: Kotlin can convert a lambda to a Java functional interface automatically via SAM conversion when the target type is known. Where the target type cannot be inferred (for example when returning or storing the interface), you’ll use a SAM constructor like Runnable { ... } to create the Java-compatible instance.
You will learn lambdas with receivers: lambdas where this refers to a supplied receiver object so members can be called without qualification. You will use with to call several methods on one object and apply to configure an object and return it, and you will be able to explain how these patterns enable concise DSL-like code.
After completing the unit you’ll be comfortable writing idiomatic Kotlin lambdas, choosing between eager collections and lazy sequences, using member references to simplify forwarding lambdas, integrating with Java functional interfaces (including when to use SAM constructors), and employing lambdas with receivers (with/apply) to structure concise, object-scoped code. The next step is to study Kotlin’s type system (nullability, primitives, and collection type distinctions).
Materials
Source document
- Kotlin in Action, Second Edition, Sebastian Aigner, Roman Elizarov, Svetlana Isakova, and Dmitry Jemerov, Manning, April 2024 — Link — Page 119-153