Book Image

Kotlin Standard Library Cookbook

By : Samuel Urbanowicz
Book Image

Kotlin Standard Library Cookbook

By: Samuel Urbanowicz

Overview of this book

For developers who prefer a more simplistic approach to coding, Kotlin has emerged as a valuable solution for effective software development. The Kotlin standard library provides vital tools that make day-to-day Kotlin programming easier. This library features core attributes of the language, such as algorithmic problems, design patterns, data processing, and working with files and data streams. With a recipe-based approach, this book features coding solutions that you can readily execute. Through the book, you’ll encounter a variety of interesting topics related to data processing, I/O operations, and collections transformation. You’ll get started by exploring the most effective design patterns in Kotlin and understand how coroutines add new features to JavaScript. As you progress, you'll learn how to implement clean, reusable functions and scalable interfaces containing default implementations. Toward the concluding chapters, you’ll discover recipes on functional programming concepts, such as lambdas, monads, functors, and Kotlin scoping functions, which will help you tackle a range of real-life coding problems. By the end of this book, you'll be equipped with the expertise you need to address a range of challenges that Kotlin developers face by implementing easy-to-follow solutions.
Table of Contents (11 chapters)

What this book covers

Chapter 1, Ranges, Progressions, and Sequences, presents the concept of Kotlin ranges and sequences. It shows how to approach common algorithmic problems by defining custom sequences and how to define ranges for custom classes.

Chapter 2, Expressive Functions and Adjustable Interfaces, shows how to approach designing functions and interfaces using the language's built-in features. The chapter explains how to implement clean, reusable functions, and scalable interfaces containing default implementations. The recipes also cover other features of the language, such as inlining closures, destructuring variables, reified type parameters, and other useful tips that help design more flexible and natural code.

Chapter 3, Shaping Code with Kotlin Functional Programming Features, shows how to solve real-life programming challenges by adopting state-of-the-art functional programming patterns. The chapter helps readers get familiar with Kotlin support for functional programming concepts provided by the standard library and the built-in language features.

Chapter 4, Powerful Data Processing, focuses on presenting standard library support for declarative style operations on collections. The included recipes present solutions to varying programming problems related to dataset transformations, reducing, or filtering. The chapter shows how to approach data processing operations practicing a functional programming style with the use of powerful functionalities built into the standard library.

Chapter 5, Tasteful Design Patterns Adopting Kotlin Concepts, presents the Kotlin-specific approach to implementing popular design patterns, including the Observer and Lazy delegates, Builder, Strategy, and more. Design patterns presented in the following chapter are backed by real-life examples that emphasize the benefits of the Delegation pattern.

Chapter 6, Friendly I/O Operations, presents useful extension functions available in the standard library that simplify the work with I/O operations. The chapter focuses on common use cases of read-write operations on files, working with streams and buffers, and the Kotlin approach to traversing files available in a specific directory.

Chapter 7, Making Asynchronous Programming Great Again, is an in-depth guide to asynchronous programming focusing on the Kotlin coroutines framework and its application in real-life situations. The chapter presents how to optimize and advance previous examples by executing parts of their code in the background in a non-blocking way. Here, you will also find a practical example of implementing an asynchronous REST client with the Retrofit library and the coroutines framework.

Chapter 8, Best Practices for the Android, JUnit, and JVM UI Frameworks, covers practical problems specific to the popular frameworks, among which Kotlin is the one used most often. In general, it will focus on Android platform-specific aspects and asynchronous UI programming with coroutines, both on Android and JVM frameworks such as JavaFX and Swing. It will also help you to write effective unit tests for the JVM platform using the JUnit framework. The recipes related to unit testing will also include more advanced topics, such as mocking dependencies with the Mockito Kotlin library and testing asynchronous code based on the coroutines framework.

Chapter 9, Miscellaneous, presents handy solutions to various problems and issues that Kotlin developers deal with on a daily basis.