kt.academy Logo

Functional Kotlin:

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.

Who is this book for

This book is perfect for developers who know the basics of Kotlin and want to learn more advanced features. Math knowledge is NOT required to understand and benefit from this book. It is a natural continuation of Kotlin Essentials.

What you will learn

This book focuses on Kotlin’s functional features, including: - Generic variance modifiers - Covariant Nothing Object pattern - Generic variance modifier limitations - Interface delegation - Implementing custom property delegate - Property delegates from Kotlin stdlib - Kotlin Contracts - Kotlin and Java type mapping - Annotations for Kotlin and Java interoperability - Multiplatform development structure, concepts and possibilities - Implementing multiplatform library - Implementing Android and iOS applications with shared module - Essentials of Kotlin/JS - Reflecting Kotlin elements - Reflecting Kotlin types - Implementing custom Annotation Processor - Implementing custom Kotlin Symbol Processor - KSP incremental compilation and multiple round processing - Defining Compiler Plugin - Core Static Analysis concepts - Overview of Kotlin static analyzers - Defining custom Detekt rule This book is full of example projects, including: - Type-safe task update class using Covariant Nothing Object pattern (Generic variance modifiers chapter) - Logging property delegate (Property delegation chapter) - Object serializer (Reflection chapter) - Random value generator for generic types (Reflection chapter) - Annotation Processor generating interface for a class (Annotation Processing chapter). - Kolin Symbol Processor generating interface for a class (Kotlin Symbol Processing chapter). - Detekt rule finding <code>System.out.println</code> usage.

Chapter by Chapter

Clear path to progress

1.Introduction
  • Who is this book for?
  • What will be covered?
  • The Kotlin for Developers series
  • Conventions
  • Code conventions
  • Exercises and solutions
  • Acknowledgments
2.Introduction to functional programming with Kotlin
  • Why do we need to use functions as objects?
3.Function types
  • Defining function types
  • Using function types
  • Named parameters
  • Type aliases
  • A function type is an interface
4.Anonymous functions
    5.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
    6.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
    7.SAM Interface support in Kotlin
    • Support for Java SAM interfaces in Kotlin
    • Functional interfaces
    8.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
    9.Collection processing
    • 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
    10.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
    11.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
    12.Scope functions
    • let
    • also
    • takeIf and takeUnless
    • apply
    • The dangers of careless receiver overloading
    • with
    • run
    • Using scope functions
    • Exercise: Using scope functions
    • Exercise: orThrow
    13.Context parameters
    • Extension function problems
    • Introducing context parameters
    • Use cases
    • Concerns
    • Named context parameters
    • Summary
    • Exercise: Logger
    14.A birds-eye view of Arrow
    • Functions and Arrow Core
    • Testing higher-order functions
    • Error Handling
    • Data Immutability with Arrow Optics
    15.Final words
    • Final Project: UserService
    16.Exercise solutions
      17.Notes