Introduction
- Who is this book for?
- What will be covered?
- The Kotlin for Developers series
- Conventions
- Code conventions
- Exercises and solutions
- 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
- Exercise: Function types and literals
- Exercise: Observable value
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
- Exercise: Inferred function types
- Exercise: Function 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
- Exercise: Inline functions
Collection processing
forEachandonEachfiltermapmapNotNullflatMap- Exercise: Implement map
- Exercise: Optimize collection processing
foldreducesumwithIndexand indexed variantstake,takeLast,drop,dropLastandsubList- Exercise: Adding element at position
- Getting elements at certain positions
- Finding an element
- Counting elements
any,allandnone- Exercise: Implement shop functions
partitiongroupBy- Associating to a map
distinctanddistinctBy- Exercise: Prime access list
- Sorting:
sorted,sortedByandsortedWith - Sorting mutable collections
- Maximum and minimum
shuffledandrandom- Exercise: Top articles
- Exercise: Refactor collection processing
zipandzipWithNext- Windowing
joinToStringMap,SetandStringprocessing- Exercise: Passing students list
- Exercise: Best students list
- Exercise: Functional Quick Sort
- Exercise: Powerset
- Exercise: All possible partitions of a set
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
- Exercise: Understanding sequences
Type Safe DSL Builders
- 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
Scope functions
letalsotakeIfandtakeUnlessapply- The dangers of careless receiver overloading
withrun- Using scope functions
- Exercise: Using scope functions
- Exercise: orThrow
Context parameters
- Extension function problems
- Introducing context parameters
- Use cases
- Concerns
- Named context parameters
- Summary
- Exercise: Logger
A birds-eye view of Arrow
- Functions and Arrow Core
- Testing higher-order functions
- Error Handling
- Data Immutability with Arrow Optics
Final words
- Final Project: UserService
