Functional Kotlin
eBook - ePub

Functional Kotlin

  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Functional Kotlin

About this book

Learn how to apply Functional Programming with Kotlin to real-life projects with popular libraries like Arrow.

Key Features

  • Focus on the functional aspects of Kotlin and identify the advantages that functional programming brings to the table and the associated coding benefits.
  • Implement common functional programming design patterns and techniques.
  • Learn to combine OOP and Reactive Programming with Functional Programming and how RxKotlin and funkTionale can help you implementing Functional Programming in Kotlin

Book Description

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.

What you will learn

  • Learn the Concepts of Functional Programming with Kotlin
  • Discover the Coroutines in Kotlin
  • Uncover Using funkTionale plugin
  • Learn Monads, Functiors and Applicatives
  • Combine Functional Programming with OOP and Reactive Programming
  • Uncover Using Monads with funkTionale
  • Discover Stream Processing

Who this book is for

Kotlin developers who have no functional programming experience, will benefit from this book.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Asynchronous Programming with Coroutines

Today's software development landscape makes asynchronous processing one of the most important topics. The ever-increasing number of processors and cores and the massive consumption of external services (which has grown in recent years with the adoption of microservices architectures) are some of the factors that we should keep an eye on and strive to use a good asynchronous approach.
Kotlin's implementation of coroutines is an excellent tool to build asynchronous applications.
In this chapter, we'll cover the following topics:
  • Coroutines
  • Alternative approaches
  • Asynchronous processing
  • Channels and actors

Introduction to coroutines

Let's start with a simple example without coroutines:
import kotlin.concurrent.thread

fun
main(args: Array<String>) {
thread {
Thread.sleep(1000)
println("World!")
}
print("Hello ")
Thread.sleep(2000)
}
The thread function executes a block of code in a different thread. Inside the block, we are simulating an expensive I/O computation (such as accessing data from a microservice over HTTP) with Thread.sleep. Thread.sleep will block the current thread for the number of milliseconds passed as a parameter. In this example, we don't wait until the computation finishes to keep working on other things; we print another message, "Hello", while the other computation is being executed. At the end, we wait for two seconds until the computation is finished.
That's not a pretty code, and we can do better:
fun main(args: Array<String>) {
val computation = thread {
Thread.sleep(1000)
println("World!")
}
print("Hello ")
computation.join()
}
In this version, we have a reference to our thread called, computation; at the end, we wait for the join() method to finish. This is smarter than just waiting for a fixed amount of time, as real-life computations could have different execution times.

Understanding JVM threads

Threads are the building blocks of asynchronous concurrent applications on JVM (and other platforms, too). A JVM thread is, most of the time, backed by a hardware thread (such as a core inside a processor). A hardware thread can support several software threads (a JVM thread is a kind of software thread), but only one software thread is executed at any given time.
The OS (or the JVM) decides which software thread is executed on each hardware thread and switches quickly among the live threads, thereby, giving the appearance that there are several software threads executing at the same time, when in reality there are as many active software threads being executed as there are hardware threads. But, in most circumstances, it is useful to think that all software threads are being performed at the same time.
Threads in JVM are very fast and responsive, but they come at a cost. Each ...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. Kotlin – Data Types, Objects, and Classes
  8. Getting Started with Functional Programming
  9. Immutability - It's Important
  10. Functions, Function Types, and Side Effects
  11. More on Functions
  12. Delegates in Kotlin
  13. Asynchronous Programming with Coroutines
  14. Collections and Data Operations in Kotlin
  15. Functional Programming and Reactive Programming
  16. Functors, Applicatives, and Monads
  17. Working with Streams in Kotlin
  18. Getting Started with Arrow
  19. Arrow Types
  20. Kotlin's Quick Start
  21. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Functional Kotlin by Mario Arias, Rivu Chakraborty in PDF and/or ePUB format, as well as other popular books in Computer Science & Object Oriented Programming. We have over one million books available in our catalogue for you to explore.