Functional Kotlin

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.

Introduction

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.

Who this book is 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 for Developers: Essentials.

What is covered

This book focuses on Kotlin’s functional features, including:

  • function types,
  • anonymous functions,
  • lambda expressions,
  • function references,
  • functional interfaces,
  • collection processing functions,
  • sequences,
  • DSL usage and creation,
  • scope functions,
  • the essentials of the Arrow library.
Marcin Moskała

Marcin Moskała

Marcin Moskala is an experienced developer and Kotlin trainer. He is the founder of the Kt. Academy, an official JetBrains partner for Kotlin training, author of the books Effective Kotlin, Kotlin Coroutines, Functional Kotlin and Android Development with Kotlin. He is also the main author of the biggest medium publication about Kotlin and a speaker invited to many programming conferences.

Chapters available as articles

Function references

How do we reference functions in Kotlin, to replace lambda expressions. All functions, including constructors, getters, extensions and more.

Function types

What is a function type, and what less known features it offers.

Lambda expressions

Let's learn how to use lambda expressions, what special support they have, and what traps they generate.

Collection processing in Kotlin: Basic functions

One of the most useful applications of functional programming is collection processing. Let's learn about essential collection processing functions. That is also a start of a series of articles about functional collection processing functions.

Collection processing in Kotlin: Folding and reducing

Folding stands behind nearly all collection processing functions. fold and reduce are our super agents, we can use whenever we cannot find more specific collection processing function.

Collection processing in Kotlin: Using indices

Just because we use use functional processing function, does not stop us from using indices. Let's learn about withIndex and indexed variants of important functions.

Collection processing in Kotlin: drop and take

How to take the first n elements? Of all except the first n? Let's learn about drop, take, dropLast and takeLast.

Collection processing in Kotlin: Finding, counting, and checking conditions

Let's learn about finding elements, counting those that fulfill a predicate, or checking predicates for all elements.

Collection processing in Kotlin: Grouping elements

How do we group collection elements in Kotlin, and how is that useful in a variety of algorithms.

Collection processing in Kotlin: Associating elements

How do we transform collections into maps using associating functions.

Collection processing in Kotlin: Sorting, shuffling and reversing

Sorting elements using their order, or by key, or by multiple keys... Learn about sorting collections in Kotlin.

Collection processing in Kotlin: Windowing, zipping and chunking

Let's learn about two most mysterious collection processing functions: windowing and chunking.

Collection processing in Kotlin: Ending

The closing article of the collection processing series, where we will learn about joinToString, and general ideas regarding processing different kind of objects in Kotlin.

DSL type-safe builders

How to define type-safe DSL builders in Kotlin.

Scope functions

What is let, also, apply and with, and how to use them in our applications.

Context receivers

All you need to know about the new and powerful Kotlin feature: context receivers.

A birds-eye view of Arrow: working with function with Arrow Core

Learn about essential Arrow functions used for composition, memoization, recursion and functions generation.

A birds-eye view of Arrow: Error Handling

Learn about Error Handling in Arrow, end how to effectively work with Either.