Functional Kotlin:

Kotlin is a powerful language that has already revolutionized Android and backend development, but it also has powerful support for functional programming. Its amazing features, like functional collection processing, DSL builders, scope functions, or context receivers, help developers achieve intuitive and expressive functional syntax. Additionally, Kotlin’s extensibility allows libraries like Arrow to promote an even more functional style, all while keeping proper type safety, stability, and performance. Thanks to all this, Kotlin has become the promised land for many developers who are passionate about functional programming in real-life applications.
This book is about Kotlin’s functional programming features. It covers the essentials first and then builds on them. It presents important and practical topics like collection processing, function references, scope functions, DSL usage and creation, and context receivers.
The purpose

Chapter by Chapter
Clear path to progress
- Who is this book for?
- What will be covered?
- The Kotlin for Developers series
- Conventions
- Code conventions
- Exercises and solutions
- Acknowledgments
- Why do we need to use functions as objects?
- Defining function types
- Using function types
- Named parameters
- Type aliases
- A function type is an interface
- Tricky braces
- Parameters
- Trailing lambdas
- Result values
- Lambda expression examples
- An implicit name for a single parameter
- Closures
- Lambda expressions vs anonymous functions
- Exercise: Function types and literals
- Exercise: Observable value
- 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
- Exercise: Inferred function types
- Exercise: Function references
- Support for Java SAM interfaces in Kotlin
- Functional interfaces
- 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
- Exercise: Inline functions
- forEach and onEach
- filter
- map
- mapNotNull
- flatMap
- Exercise: Implement map
- Exercise: Optimize collection processing
- fold
- reduce
- sum
- withIndex and indexed variants
- take, takeLast, drop, dropLast and subList
- Exercise: Adding element at position
- Getting elements at certain positions
- Finding an element
- Counting elements
- any, all and none
- Exercise: Implement shop functions
- partition
- groupBy
- Associating to a map
- distinct and distinctBy
- Exercise: Prime access list
- Sorting: sorted, sortedBy and sortedWith
- Sorting mutable collections
- Maximum and minimum
- shuffled and random
- Exercise: Top articles
- Exercise: Refactor collection processing
- zip and zipWithNext
- Windowing
- joinToString
- Map, Set and String processing
- Exercise: Passing students list
- Exercise: Best students list
- Exercise: Functional Quick Sort
- Exercise: Powerset
- Exercise: All possible partitions of a set
- 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
- Exercise: Understanding sequences
- A function type with a receiver
- Simple DSL builders
- Using apply
- Simple DSL-like builders
- Multi-level DSLs
- DslMarker
- A more complex example
- When should we use DSLs?
- Summary
- Exercise: HTML table DSL
- Exercise: Creating user table row
- let
- also
- takeIf and takeUnless
- apply
- The dangers of careless receiver overloading
- with
- run
- Using scope functions
- Exercise: Using scope functions
- Exercise: orThrow
- Extension function problems
- Introducing context parameters
- Use cases
- Concerns
- Named context parameters
- Summary
- Exercise: Logger
- Functions and Arrow Core
- Testing higher-order functions
- Error Handling
- Data Immutability with Arrow Optics
- Final Project: UserService