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)

Implementing the advanced Observer pattern by defining a custom property delegate

In this recipe, we are going to implement a custom, generic property delegate combining features of the Observable and Vetoable delegates available in the standard library. In other words, we want to implement a property delegate that allows us to notify a subscribed listener about any changes made to the observed property. At the same time, we also want the delegate to allow filtering of the updates made to the delegated property. In this example, we are going to declare the temperature: Int variable delegated to our custom implementation of the ObservableVetoable delegate class. We are going to create a generic class that allows us to pass the initial value, a function responsible for filtering property updates and a function that will be invoked immediately after the change to the property is...