Functional Programming in Kotlin
Sustainable code with Kotlin and Arrow
Marco Vermeulen, Rúnar Bjarnason, and Paul Chiusano
  • MEAP began August 2019
  • Publication in Spring 2021 (estimated)
  • ISBN 9781617297168
  • 325 pages (estimated)
  • printed in black & white

This is a fantastic book if you want to finally get what's the fuss about FP.

Felipe Fernández
Functional Programming in Kotlin is a reworked version of the bestselling Functional Programming in Scala, with all code samples, instructions, and exercises translated into the powerful Kotlin language. In this authoritative guide, you’ll take on the challenge of learning functional programming from first principles, and start writing Kotlin code that’s easier to read, easier to reuse, better for concurrency, and less prone to bugs and errors.

About the Technology

Kotlin is a new JVM language designed to interoperate with Java and offer an improved developer experience for creating new applications. It’s already a top choice for writing web services, and Android apps. Although it preserves Java’s OO roots, Kotlin really shines when you adopt a functional programming mindset. By learning the core principles and practices of functional programming outlined in this book, you’ll start writing code that’s easier to read, easier to test and reuse, better for concurrency, and less prone to bugs.

About the book

Functional Programming in Kotlin is a serious tutorial for programmers looking to learn FP and apply it to the everyday business of coding. Based on the bestselling Functional Programming in Scala, this book guides intermediate Java and Kotlin programmers from basic techniques to advanced topics in a logical, concise, and clear progression. In it, you'll find concrete examples and exercises that open up the world of functional programming. The book will deliver practical mastery of FP using Kotlin and a valuable perspective on program design that you can apply to other languages.
Table of Contents detailed table of contents

Part 1: Introduction to Functional Programming

1 What is functional programming?

1.1 The benefits of FP: a simple example

1.1.1 A program with side effects

1.1.2 A functional solution: removing the side effects

1.2 Exactly what is a (pure) function?

1.3 Referential transparency, purity, and the substitution model

1.4 What lies ahead

1.5 Summary

2 Getting started with functional programming in Kotlin

2.1 Higher-order functions: passing functions to functions

2.1.1 A short detour: writing loops functionally

2.1.2 Writing our first higher-order function

2.2 Polymorphic functions: abstracting over types

2.2.1 An example of a polymorphic function

2.2.2 Calling HOFs with anonymous functions

2.3 Following types to implementations

2.4 Summary

3 Functional data structures

3.1 Defining functional data structures

3.2 Working with functional data structures

3.2.1 The when construct for matching by type

3.2.2 The when construct as alternative to if-else logic

3.2.3 Pattern matching and how it differs from Kotlin matching

3.3 Data sharing in functional data structures

3.3.1 The efficiency of data sharing

3.4 Recursion over lists and generalizing to higher-order functions

3.4.1 More functions for working with lists

3.4.2 Lists in the Kotlin standard library

3.4.3 Inefficiency on assembling list functions from simpler components

3.5 Trees

3.6 Summary

4 Handling error without exceptions

4.1 The good and bad aspects of exceptions

4.2 Possible alternatives to exceptions

4.2.1 Sentinel value

4.2.2 Supplied default value

4.3 Encoding success conditions with Option

4.3.1 Usage patterns for Option

4.3.2 Option composition, lifting, and wrapping exception-oriented APIs

4.3.3 For comprehensions with Option using Arrow fx

4.4 Encoding success and failure conditions with Either

4.4.1 For comprehensions with Either using Arrow fx

4.5 Summary

5 Strictness and laziness

5.1 Strict and non-strict functions

5.2 An extended example: lazy lists

5.2.1 Memoizing of streams and avoiding recomputation

5.2.2 Helper functions for inspecting streams

5.3 Separating program description from evaluation

5.4 Producing infinite data streams through corecursive functions

5.5 Summary

6 Purely functional state

6.1 Generating random numbers using side effects

6.2 Purely functional random number generation

6.3 Making stateful APIs pure

6.4 An implicit approach to passing state actions

6.4.1 More power by combining state actions

6.4.2 Recursive retries through nested state actions

6.4.3 Applying the combinator API to the initial example

6.5 A general state action data type

6.6 Purely functional imperative programming using Arrow

6.7 Conclusion

6.8 Summary

Part 2: Functional design and combinator libraries

7 Purely functional parallelism

7.1 Choosing data types and functions

7.1.1 A data type for parallel computations

7.1.2 Combining parallel computations to ensure concurrency

7.1.3 Marking computations to be forked explicitly

7.2 Picking a representation

7.3 Refining the API with the end user in mind

7.4 Reasoning about the API in terms of algebraic equations

7.4.1 The law of mapping

7.4.2 The law of forking

7.4.3 Using actors for a non-blocking implementation

7.5 Refining combinators to their most general form

7.6 Summary

8 Property-based testing

8.1 A brief tour of property-based testing

8.2 Choosing data types and functions

8.2.1 Gathering initial snippets for a possible API

8.2.2 Exploring the meaning and API of properties

8.2.3 Discovering the meaning and API of generators

8.2.4 Generators that depend on generated values

8.2.5 Refining the property data type

8.3 Test case minimization

8.4 Using the library and improving user experience

8.4.1 Some simple examples

8.4.2 Writing a test suite for parallel computations

8.5 Testing higher-order functions and other possibilities

8.6 The laws of generators

8.7 Summary

9 Parser combinators

9.1 Designing an algebra

9.1.1 A parser to recognize single characters

9.1.2 A parser to recognize entire strings

9.1.3 A parser to recognize repetition

9.2 One possible approach to designing an algebra

9.2.1 Counting character repetition

9.2.2 Slicing and nonempty repetition

9.3 Handling context sensitivity

9.4 Writing a JSON parser

9.4.1 Defining expectations of a JSON parser

9.4.2 Reviewing the JSON format

9.4.3 A JSON parser

9.5 Surfacing errors through reporting

9.5.1 First attempt at representing errors

9.5.2 Accumulating errors through error nesting

9.5.3 Controlling branching and backtracking

9.6 Implementing the algebra

9.6.1 Building up the algebra implementation gradually

9.6.2 Sequencing parsers after each other

9.6.3 Capturing error messages through labeling parsers

9.6.4 Recovering from error conditions and backtracking over them

9.6.5 Propagating state through context-sensitive parsers

9.7 Summary

Part 3: Common Structures in Functional Design

10 Monoids

10.1 What is a monoid?

10.2 Folding lists with monoids

10.3 Associativity and parallelism

10.4 Example: Parallel parsing

10.5 Foldable data structures

10.6 Composing monoids

10.6.1 Assembling more complex monoids

10.6.2 Using composed monoids to fuse traversals

10.7 Summary

11 Monads

12 Applicative and traversable functors

Part 4: Effects and I/O

13 External effects and I/O

14 Local effects and mutable state

15 Stream processing and incremental I/O

Appendixes

Appendix A: Appendix A: Exercise hints and tips

A.1 Introduction

A.2 Functional data structures

A.3 Handling error without exceptions

A.4 Strictness and laziness

A.5 Purely functional state

A.6 Purely functional parallelism

A.7 Property-based testing

A.8 Parser combinators

A.9 Monoids

A.10 Monads and functors

A.11 Applicative and traversable functors

Appendix B: Appendix B: Exercise solutions

B.1 Before you proceed to the solutions

B.2 Getting started with functional programming

B.3 Functional data structures

B.4 Handling error without exceptions

B.5 Strictness and laziness

B.6 Purely functional state

B.7 Purely functional parallelism

B.8 Property-based testing

B.9 Parser combinators

B.10 Monoids

B.11 Monads and functors

B.12 Applicative and traversable functors

What's inside

  • Functional programming techniques for real-world applications
  • Write combinator libraries
  • Identify common structures and idioms in functional design
  • Code for simplicity, modularity, and fewer bugs

About the reader

For intermediate Kotlin and Java developers. No experience with functional programming is required.

About the author

Marco Vermeulen has almost two decades of programming experience on the JVM, with much of that time spent on functional programming using Scala and Kotlin.

Rúnar Bjarnason and Paul Chiusano are the authors of Functional Programming in Scala, on which this book is based. They are internationally-recognized experts in functional programming and the Scala programming language.

placing your order...

Don't refresh or navigate away from the page.
Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
print book $29.99 $49.99 pBook + eBook + liveBook
Additional shipping charges may apply
Functional Programming in Kotlin (print book) added to cart
continue shopping
go to cart

eBook $31.99 $39.99 3 formats + liveBook
Functional Programming in Kotlin (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.
customers also reading

This book 1-hop 2-hops 3-hops

FREE domestic shipping on three or more pBooks