Android & Kotlin

Functional Programming in Kotlin by Tutorials

Learn to handle errors and state changes in a safe way using functional programming fundamentals. By Massimo Carli.

Read for Free with the Personal Plan* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.

5 (1) · 1 Review

Download materials
Buy paperback—Amazon Comments
Save for later
Share

Who is this for?

This book is for Kotlin engineers who want to learn functional programming to solve complex problems in a fun, practical, hands-on way.

Covered concepts

  • What is functional programming and what are the benefits.
  • The fundamental concepts of functional programming, such as pure and higher order functions.
  • Functional data structures and how to use them.
  • The Kotlin language features that help us apply the functional programming paradigm.
  • How to use functional programming to manage errors and state changes in real-world applications.
  • Some of the libraries and tools available to make getting started with functional programming a breeze.

Functional programming is a powerful paradigm for building your applications. This book will teach you the fundamentals of functional programming in a practical, hands-on way, while showing you how you can safely handle errors and state changes in your programs.

more

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

Section I: Functional Programming Fundamentals

This is the very start of your functional programming journey. Here, you’ll learn what functional programming is and why it’s useful as well as its fundamental concepts. Using hands-on examples every step of the way, you’ll discover pure functions, higher-order functions, composition and so much more!

1
Toggle description
Functional programming is a programming paradigm where programs are constructed by applying and composing functions. In this chapter, you'll learn why functional programming is important and how it can make your code more robust, readable and testable.
2
Toggle description
In this chapter, you'll start thinking functional. You'll be introduced to category theory, and you'll learn how the category of types and functions relates to functional programming. From the concepts of initial and terminal object of a category, you'll understand where some of the Kotlin fundamental types come from — all this while having fun with logic and exercises.
3
Toggle description
Not all functions are the same. In this chapter, you'll learn what pure functions are and why they're important. You'll understand the concepts of referential transparency and, using the substitution model, you'll learn how to validate the purity of a function. In the last part of the chapter, you'll start learning the basics of another fundamental concept: side effects.
Lazy evaluation and memoization are two of the most important advantages you get when writing pure functions. In this chapter, you'll learn what an evaluation strategy is and how to achieve lazy evaluation using lambda expressions.
5
Toggle description
Functional programming allows you to have a declarative approach in the creation of reusable and more readable code. This is possible through the use of higher-order functions, the topic of this chapter.
6
Toggle description
Immutability and recursion are concepts you need to understand for implementing functional data structures. In this chapter, you'll learn why immutability is something you should pursue and what the challenges are when using immutable code.
Toggle description
Functional data structures and, in particular, persistent data structures are fundamental tools for implementing functional programming code. In this chapter, you'll learn what they are, and you'll implement the main function of a singly linked list.
Toggle description
Composition is the most important concept of functional programming but not the easiest to understand and implement. In this chapter, you'll learn how to compose pure functions as well as functions with side effects.

Section II: Data Types & Typeclasses

In this section, you’ll learn some of the types you see when using functional programming. You’ll see concepts you likely already know, like “List” and “Optional”, while discovering some of their very important properties as they relate to functional programming.

Toggle description
Data types are fundamental tools in the implementation of programs using the functional programming approach. In this chapter, you'll learn all about data types, their analogies with the concept of containers and how you can interact with their content in a safe and pure way.
Toggle description
Functional programming has a strong relationship with math. In this chapter, you'll learn what algebra is and how to apply some basic math principles to important functional programming concepts like currying.
Toggle description
Functors map objects and morphisms in one category to objects and morphisms in another while preserving their structures. But what does this really mean, and what are the practical consequences? This chapter will answer this and other questions about functors.
Toggle description
Monoids and semigroups are two very simple but powerful typeclasses you frequently find and use in your code. In this chapter, you'll learn all about them and see how to prove their laws using property-based testing.
Toggle description
Monads are one of the most challenging but rewarding concepts in functional programming. They're how you compose functions handling side effects. In this chapter, you'll understand what they are, and more importantly, how to use them.

Section III: Functional Programming in Practice

Time to put all that knowledge to work. In this section, you’ll use all the principles you’ve learned so far to see the functional programming way for handling errors and managing state changes. You’ll also meet some libraries you can use in your projects to quickly introduce functional programming.

Handling errors and exceptions is what makes your software reliable. In this chapter, you'll see how to handle exceptions using all the functional programming concepts you learned in the first two sections of the book.
Toggle description
In this chapter, you'll learn all about the state data type as a way to control the side effects of an impure function.
Toggle description
Controlling side effects is one of the most important topics in functional programming. In this chapter, you'll learn how to handle them using the IO monad.
Toggle description
Lists, sequences and flows are probably tools you use every day in your job. In this chapter, you'll learn what functional programming principles they follow and how to make them even more powerful.
In this chapter, you'll see how the functional programming principles you learned in the previous chapters are used in the creation of Mobius, an interesting implementation of a unidirectional flow architecture.
Toggle description
Arrow is one of the most important libraries that allows you to apply functional programming concepts to your Kotlin code. In this chapter, you'll get a first taste of Arrow and how it can help you write pure code in your project.

Appendix

This book has many exercises and challenges throughout. In this appendix, you’ll find solutions to all exercises and challenges, along with additional explanations about the answers.