Kotlin Standard Library Cookbook

Book Description

Build optimized applications in Kotlin by learning how to smartly implement standard libraries.

Key Features

  • Get the most out of the Kotlin library to develop high-quality portable applications
  • Explore the powerful support for data processing and I/O operations
  • Discover ways to enhance your Android application development

Book Description

Given the verbosity of Java, developers have turned to Kotlin for effective software development. The Kotlin standard library provides vital tools that make day-to-day Kotlin programming easier. This library features the core attributes of the language, such as algorithmic problems, design patterns, data processing, and working with files and data streams.

The recipes in this book offer coding solutions that can be readily executed. The book covers various topics related to data processing, I/O operations, and collections transformation. We'll walk through effective design patterns in Kotlin and you'll understand how coroutines add new features to JavaScript.

As you make your way through the chapters, you'll learn how to implement clean, reusable functions and scalable interfaces containing default implementations. In the concluding chapters, we'll provide recipes on functional programming concepts, such as lambdas, monads, functors, and Kotlin scoping functions.

By the end of the book, you'll be able to address a range of problems that Kotlin developers face by implementing easy-to-follow solutions.

What you will learn

  • Work with ranges, progressions, and sequences in use cases
  • Add new functionalities to current classes with Kotlin extensions
  • Understand elements such as lambdas, closures, and monads
  • Build a REST API consumer with Retrofit and a coroutine adapter
  • Discover useful tips and solutions for making your Android projects
  • Explore the benefits of standard library features

Who this book is for

This book is for software developers who are familiar with Kotlin's basics and want to discover more advanced features and concepts, especially those provided by the Kotlin standard library. It's also ideal for experienced software developers who are familiar with the functional programming paradigm and other programming languages who want to switch to Kotlin. It will also help Java developers switch to Kotlin and integrate it into existing Java Virtual Machine (JVM) projects.

Publisher Resources

Download Example Code

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Kotlin Standard Library Cookbook
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Get in touch
      1. Reviews
  7. Ranges, Progressions, and Sequences
    1. Introduction
    2. Exploring the use of range expressions to iterate through alphabet characters
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Traversing through ranges using progression with a custom step value
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Building custom progressions to traverse dates
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using range expressions with flow control statements
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Discovering the concept of sequences
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Applying sequences to solve algorithmic problems
      1. Getting ready
      2. How to do it...
      3. How it works...
  8. Expressive Functions and Adjustable Interfaces
    1. Introduction
    2. Declaring adjustable functions with default parameters
      1. How to do it...
      2. How it works...
      3. See also
    3. Declaring interfaces containing default implementations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Extending functionalities of classes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Destructuring types
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Returning multiple data
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    7. Inlining parameters of closure type
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    8. Infix notations for functions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Smart types checking with generic reified parameters
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Overloading operators
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
  9. Shaping Code with Kotlin Functional Programming Features
    1. Introduction
    2. Working effectively with lambda expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Discovering basic scoping functions – let, also, apply
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Initializing objects the clean way using the run scoping function
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Working with higher-order functions
      1. How to do it...
      2. How it works...
    6. Functions currying 
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. Function composition
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Implementing the Either Monad design pattern
      1. How to do it...
      2. How it works...
      3. There's more...
    9. Approach to automatic functions memoization 
      1. How to do it...
      2. How it works...
      3. There's more...
  10. Powerful Data Processing
    1. Introduction
    2. Composing and consuming collections the easy way
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Filtering datasets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Automatic null removal
      1. How to do it...
      2. How it works...
      3. See also
    5. Sorting data with custom comparators
      1. Getting started
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Building strings based on dataset elements
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    7. Dividing data into subsets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Data transformation with map and flatMap
      1. How to do it...
      2. How it works...
      3. There's more...
    9. Folding and reducing data sets
      1. How to do it...
      2. How it works...
      3. There's more...
    10. Grouping data
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
  11. Tasteful Design Patterns Adopting Kotlin Concepts
    1. Introduction
    2. Implementing the Strategy pattern
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    3. Exploring the power of the Delegation pattern
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Implementing delegated class properties
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Tracking state with the Observer pattern
      1. How to do it...
      2. How it works...
      3. See also
    6. Restricting property updates with the Vetoable delegate
      1. How to do it...
      2. How it works...
      3. See also
    7. Implementing the advanced Observer pattern by defining a custom property delegate
      1. How to do it...
      2. How it works...
      3. See also
    8. Working with the Lazy delegate
      1. How to do it...
      2. How it works...
      3. See also
    9. Implementing builders the smart way
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  12. Friendly I/O Operations
    1. Introduction
    2. Reading the contents of a file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Ensuring stream closing with the use function
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Reading the contents of a file line by line
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Writing the contents to a file
      1. How to do it...
      2. How it works...
      3. See also
    6. Appending a file
      1. How to do it...
      2. How it works...
    7. Easy file copying
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Traversing files in a directory
      1. Getting ready
      2. How to do it...
      3. How it works...
  13. Making Asynchronous Programming Great Again
    1. Introduction
    2. Executing tasks in the background using threads
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    3. Background threads synchronization
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Using coroutines for asynchronous, concurrent execution of tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Using coroutines for asynchronous, concurrent tasks execution with results handling
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Applying coroutines for asynchronous data processing
      1. How to do it...
      2. How it works...
      3. See also
    7. Easy coroutine cancelation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Building a REST API client with Retrofit and a coroutines adapter
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Wrapping third-party callback-style APIs with coroutines
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  14. Best Practices for the Android, JUnit, and JVM UI Frameworks
    1. Introduction
    2. Clean and safe view-binding with the Android Extensions plugin
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Applying coroutines for asynchronous UI programming on Android, JavaFX, and Swing
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Easy class serialization on Android using the @Parcelize annotation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Implementing a custom property delegate that provides lifecycle-aware values
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Easy operations on SharedPreferences
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Less boilerplate Cursor data parsing
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Mocking dependencies with the Mockito Kotlin library
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Verifying function invocations
      1. Getting ready
      2. How to do it...
      3. How it works...
    10. Unit tests for Kotlin coroutines
      1. Getting ready
      2. How to do it...
      3. How it works...
  15. Miscellaneous
    1. Introduction
    2. Kotlin and Java interoperability
      1. How to do it...
      2. How it works...
    3. Kotlin and JavaScript interoperability
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Renaming generated functions
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    5. Decompiling Kotlin code to Java and JVM bytecode
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Adding custom names for imports
      1. How to do it...
      2. How it works...
    7. Wrapping complex type declarations with type aliases
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Expressive try…catch declarations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    9. Safe type-casting 
      1. How to do it...
      2. How it works...
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Kotlin Standard Library Cookbook
  • Author(s): Samuel Urbanowicz
  • Release date: July 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788837668