English
Summary
Conclusions
A function type describes the parameters and result of a function that can be stored, passed, or returned, and lambdas and function references create such values. A closure captures variables of its environment, so a captured var makes the order of calls part of the behavior. inline substitutes the body of the function and its lambdas at the call site, enabling non-local return, while noinline and crossinline restrict this substitution. Lambdas with a receiver underlie builders, and the scope functions let, run, with, apply, and also differ in their receiver and result. The operations map, filter, any, firstOrNull, and sumOf express transformation, filtering, and aggregation without a manual loop. groupBy, associateBy, partition, and fold each have their own policy for repeated keys and empty data, which must be chosen deliberately. Sequences process elements lazily and allow early termination, but they are not always faster than lists. The choice between a loop, collection operations, and a sequence depends on the task, and each option is tested on an empty set, repeated keys, and the limits of numeric types.
Self-check questions
- How does a nullable function differ from a nullable result?
- What does a lambda without an explicit return return?
- When is an explicit name better than it?
- Why can a closure keep state after the factory has finished?
- What does
return@forEachmean? - How does crossinline differ from noinline?
- Which scope functions return the object itself?
- How does groupBy differ from associateBy?
- Why is fold safer than reduce for empty data?
- Which sequence operations need all the elements?
- Why can a repeated traversal repeat side effects?
- When is a functional interface better than a typealias?