This book is about functional programming features in Kotlin. It first covers the essentials and then builds on them: it presents important and practical topics like collection processing, scope functions, and DSL usage and creation, context receivers.
This book is about functional programming features in Kotlin. It first covers the essentials, and then it builds on them: it presents important and practical topics like collection processing, function references, scope functions, DSL usage and creation, and context receivers.
This position is perfect for developers who know the basics of Kotlin and want to learn well a bit more advanced features. It is a natural continuation of Kotlin for developers: Essentials.
It is based on the second day of the Kotlin for developers workshop and serves as one of the official workbooks for the workshop.
Author(s): Marcin Moskała
Publisher: leanpub.com
Year: 2022
Language: English
Pages: 258
Introduction
Who is this book for?
What will be covered?
The Kotlin for Developers series
Code conventions
Acknowledgments
Introduction to functional programming with Kotlin
Why do we need to use functions as objects?
Function types
Defining function types
Using function types
Named parameters
Type aliases
A function type is an interface
Anonymous functions
Lambda expressions
Tricky braces
Parameters
Trailing lambdas
Result values
Lambda expression examples
An implicit name for a single parameter
Closures
Lambda expressions vs anonymous functions
Function references
Top-level functions references
Method references
Extension function references
Method references and generic types
Bounded function references
Constructor references
Bounded object declaration references
Function overloading and references
Property references
SAM Interface support in Kotlin
Support for Java SAM interfaces in Kotlin
Functional interfaces
Inline functions
Inline functions
Inline functions with functional parameters
Non-local return
Crossinline and noinline
Reified type parameters
Inline properties
Costs of the inline modifier
Using inline functions
Collection processing
forEach and onEach
filter
map
flatMap
fold
reduce
sum
withIndex and indexed variants
take, takeLast, drop, dropLast and subList
Getting elements at certain positions
Finding an element
Counting: count
any, all and none
partition
groupBy
Associating: associate, associateBy and associateWith
distinct and distinctBy
Sorting: sorted, sortedBy and sortedWith
Sorting mutable collections
Maximum and minimum
shuffled and random
zip and zipWithNext
Windowing
joinToString
Map, Set and String processing
Using them all together
Sequences
What is a sequence?
Order is important
Sequences do the minimum number of operations
Sequences can be infinite
Sequences do not create collections at every processing step
When aren’t sequences faster?
What about Java streams?
Kotlin Sequence debugging
Summary
Type Safe DSL Builders
A function type with a receiver
Simple DSL builders
Using apply
Multi-level DSLs
DslMarker
A more complex example
When should we use DSLs?
Summary
Scope functions
let
also
takeIf and takeUnless
apply
The dangers of careless receiver overloading
with
run
Using scope functions
Context receivers
Extension function problems
Introducing context receivers
Use cases
Classes with context receivers
Concerns
Summary
A birds-eye view of Arrow
Functions and Arrow Core
Testing higher-order functions
Error Handling
Data Immutability with Arrow Optics
Notes