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.

The purpose

Book Purpose Arrows

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