Book Image

Functional Kotlin

Book Image

Functional Kotlin

Overview of this book

Functional programming makes your application faster, improves performance, and increases your productivity. Kotlin supports many of the popular and advanced functional features of functional languages. This book will cover the A-Z of functional programming in Kotlin. This book bridges the language gap for Kotlin developers by showing you how to create and consume functional constructs in Kotlin. We also bridge the domain gap by showing how functional constructs can be applied in business scenarios. We’ll take you through lambdas, pattern matching, immutability, and help you develop a deep understanding of the concepts and practices of functional programming. If you want learn to address problems using Recursion, Koltin has support for it as well. You’ll also learn how to use the funKtionale library to perform currying and lazy programming and more. Finally, you’ll learn functional design patterns and techniques that will make you a better programmer.By the end of the book, you will be more confident in your functional programming skills and will be able to apply them while programming in Kotlin.
Table of Contents (22 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Preface

With Google's announcement at the I/O Conference in 2017 to make Kotlin an official language for Android, Kotlin has been gaining popularity among developers around the world.

However, Kotlin's use and popularity aren't limited to the Android community. Many other communities, such as the desktop, web, and backend community are embracing Kotlin too. Many new libraries and frameworks are being created and existing ones are providing support for Kotlin.

With more developers coming to the Kotlin community and with its natural flexibility, more programming styles are being tried. The purpose of this book is to introduce the functional programming style to the wide Kotlin community, leading and guiding on the first steps, and giving the basic tools to progress further to more advanced concepts.

Who this book is for

This book is for Kotlin users (programmers, engineers, library authors, and architects) who have a basic understanding of Kotlin  and want to understand the basics ideas behind functional programming and how to use it in a practical way (if you're entirely new to Kotlin, our Appendix, Kotlin's Quick Start will give you a quick start with the language).

What this book covers

Chapter 1, Kotlin - Data Types, Objects, and Classes, introduces you to object-oriented programming in Kotlin. Kotlin is primary object-oriented programming, and we'll use these features to introduce a functional programming style.

Chapter 2, Getting Started with Functional Programming, covers the basic principles of functional programming using Kotlin's object-oriented programming features.

Chapter 3, Immutability - It's Important, emphasizes on immutability as one of the most important concepts in functional programming. This chapter will give you an in-depth understanding of immutability.

Chapter 4, Functions, Function Types, and Side Effects, introduces you to the basic functional programming concepts around functions, pure functions, and various function types and side effects.

Chapter 5, More on Functions, talks about Kotlin's features for functional programming such as extension functions, operator overloading, DSLs, and corecursion.

Chapter 6, Delegates in Kotlin, covers how Kotlin has language-level support for delegates. Though delegates are a object-oriented programming concept, they can be helpful in making your code more modular.

Chapter 7, Asynchronous Programming with Coroutines, gives you an introduction to asynchronous programming in Kotlin, comparing different styles against coroutines.

Chapter 8, Collections and Data Operations in Kotlin, covers the enhanced collections API by Kotlin and the functional interfaces that Kotlin's collections framework has to offer.

Chapter 9, Functional Programming and Reactive Programming, shows how functional programming can be combined with other programming paradigms to get the best out of them. This chapter discusses how you can combine functional programming with object-oriented programming and reactive programming.

Chapter 10, Functors, Applicatives and Monads, gives you an introduction to typed functional programming and its basic concepts. It also talks about how to implement it in Kotlin.

Chapter 11, Working with Streams in Kotlin, gets you introduced to the Streams API in Kotlin.

Chapter 12, Getting Started with Arrow, covers how to use Arrow and its extensions for functional programming, function composition, currying, partial application, memoization, and optics.

Chapter 13, Arrow Types, helps you understand Arrow data types such as Option, Either, Try, and State and its type classes, functors, and monads.

Appendix, Kotlin's Quick Start, it provides everything that you need to start writing a Kotlin code, such as tools, basic syntax constructs and other resources to help you progress in your Kotlin journey.

To get the most out of this book

The only recommended software to run and write Kotlin programs is IntelliJ IDEA (there are other ways to do it, and we cover them in the Appendix, Kotlin's Quick Start).

You can download IntelliJ IDEA from https://www.jetbrains.com/idea/download/.

You can install IntelliJ IDEA on Windows, Mac, and Linux:

  • For Windows: You can use any Windows version from XP to 10. To install it on Windows, run the installer executable and follow the instructions.
  • For Mac: You can use any macOS version starting from 10.8. To install it on macOS, mount the disk image file and copy IntelliJ IDEA.app60 to your Application folder.
  • For Linux: You can use any GNOME or KDE desktop. To install it Linux unpack the tar.gz file using the tar -xzf idea-*.tar.gz command and run the idea.sh from the bin subdirectory.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Functional-Kotlin. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/FunctionalKotlin_ColorImages.pdf .

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "We introduced a new BakeryGood class, with the shared behavior and state of both Cupcake and Biscuit classes and we made both classes extend BakeryGood."

A block of code is set as follows:

open class BakeryGood(val flavour: String) { 
  fun eat(): String { 
    return "nom, nom, nom... delicious $flavour bakery good" 
  } 
} 

class Cupcake(flavour: String): BakeryGood(flavour) 
class Biscuit(flavour: String): BakeryGood(flavour)

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

fun main(args: Array<String>) { 
    val emptyList1 = listOf<Any>()
    val emptyList2 = emptyList<Any>() 
     
    println("emptyList1.size = ${emptyList1.size}") 
    println("emptyList2.size = ${emptyList2.size}") 
} 

Any command-line input or output is written as follows:

$ kotlin HelloKt

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "A dialog will appear that will ask whether you want to open it as a file or as a project. Click on Open As Project"

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com