Kotlin Cookbook

Book description

Use Kotlin to build Android apps, web applications, and more—while you learn the nuances of this popular language. With this unique cookbook, developers will learn how to apply thisJava-based language to their own projects. Both experienced programmers and those new to Kotlin will benefit from the practical recipes in this book.

Author Ken Kousen (Modern Java Recipes) shows you how to solve problems with Kotlin by concentrating on your own use cases rather than on basic syntax. You provide the contextand this book supplies the answers. Already big in Android development, Kotlin can be used anywhere Java is applied, as well as for iOS development, native applications, JavaScriptgeneration, and more. Jump in and build meaningful projects with Kotlin today.

  • Apply functional programming concepts, including lambdas, sequences, and concurrency
  • See how to use delegates, late initialization, and scope functions
  • Explore Java interoperability and access Java libraries using Kotlin
  • Add your own extension functions
  • Use helpful libraries such as JUnit 5
  • Get practical advice for working with specific frameworks, like Android and Spring

Table of contents

  1. Foreword
  2. Preface
    1. Who Should Read This Book
    2. How This Book Is Organized
    3. Conventions Used in This Book
    4. Using Code Examples
    5. O’Reilly Online Learning
    6. How to Contact Us
    7. Acknowledgments
  3. 1. Installing and Running Kotlin
    1. 1.1. Running Kotlin Without a Local Compiler
    2. 1.2. Installing Kotlin Locally
    3. 1.3. Compiling and Running Kotlin from the Command Line
    4. 1.4. Using the Kotlin REPL
    5. 1.5. Executing a Kotlin Script
    6. 1.6. Building a Standalone Application Using GraalVM
    7. 1.7. Adding the Kotlin Plug-in for Gradle (Groovy Syntax)
    8. 1.8. Adding the Kotlin Plug-in for Gradle (Kotlin Syntax)
    9. 1.9. Using Gradle to Build Kotlin Projects
    10. 1.10. Using Maven with Kotlin
  4. 2. Basic Kotlin
    1. 2.1. Using Nullable Types in Kotlin
    2. 2.2. Adding Nullability Indicators to Java
    3. 2.3. Adding Overloaded Methods for Java
    4. 2.4. Converting Between Types Explicitly
    5. 2.5. Printing to Different Bases
    6. 2.6. Raising a Number to a Power
    7. 2.7. Using Bitwise Shift Operators
    8. 2.8. Using Bitwise Boolean Operators
    9. 2.9. Creating Pair Instances with to
  5. 3. Object-Oriented Programming in Kotlin
    1. 3.1. Understanding the Difference Between const and val
    2. 3.2. Creating Custom Getters and Setters
    3. 3.3. Defining Data Classes
    4. 3.4. The Backing Property Technique
    5. 3.5. Overloading Operators
    6. 3.6. Using lateinit for Delayed Initialization
    7. 3.7. Using Safe Casting, Reference Equality, and Elvis to Override equals
    8. 3.8. Creating a Singleton
    9. 3.9. Much Ado About Nothing
  6. 4. Functional Programming
    1. 4.1. Using fold in Algorithms
    2. 4.2. Using the reduce Function for Reductions
    3. 4.3. Applying Tail Recursion
  7. 5. Collections
    1. 5.1. Working with Arrays
    2. 5.2. Creating Collections
    3. 5.3. Creating Read-Only Views from Existing Collections
    4. 5.4. Building a Map from a Collection
    5. 5.5. Returning a Default When a Collection Is Empty
    6. 5.6. Restricting a Value to a Given Range
    7. 5.7. Processing a Window on a Collection
    8. 5.8. Destructuring Lists
    9. 5.9. Sorting by Multiple Properties
    10. 5.10. Defining Your Own Iterator
    11. 5.11. Filtering a Collection by Type
    12. 5.12. Making a Range into a Progression
  8. 6. Sequences
    1. 6.1. Using Lazy Sequences
    2. 6.2. Generating Sequences
    3. 6.3. Managing Infinite Sequences
    4. 6.4. Yielding from a Sequence
  9. 7. Scope Functions
    1. 7.1. Initializing Objects After Construction with apply
    2. 7.2. Using also for Side Effects
    3. 7.3. Using the let Function and Elvis
    4. 7.4. Using let with a Temporary Variable
  10. 8. Kotlin Delegates
    1. 8.1. Implementing Composition by Delegation
    2. 8.2. Using the lazy Delegate
    3. 8.3. Ensuring That a Value Is Not Null
    4. 8.4. Using the observable and vetoable Delegates
    5. 8.5. Supplying Maps as Delegates
    6. 8.6. Creating Your Own Delegates
  11. 9. Testing
    1. 9.1. Setting the Test Class Life Cycle
    2. 9.2. Using Data Classes for Tests
    3. 9.3. Using Helper Functions with Default Arguments
    4. 9.4. Repeating JUnit 5 Tests with Different Data
    5. 9.5. Using Data Classes for Parameterized Tests
  12. 10. Input/Output
    1. 10.1. Managing Resources with use
    2. 10.2. Writing to a File
  13. 11. Miscellaneous
    1. 11.1. Working with the Kotlin Version
    2. 11.2. Executing a Lambda Repeatedly
    3. 11.3. Forcing when to Be Exhaustive
    4. 11.4. Using the replace Function with Regular Expressions
    5. 11.5. Converting to Binary String and Back
    6. 11.6. Making a Class Executable
    7. 11.7. Measuring Elapsed Time
    8. 11.8. Starting Threads
    9. 11.9. Forcing Completion with TODO
    10. 11.10. Understanding the Random Behavior of Random
    11. 11.11. Using Special Characters in Function Names
    12. 11.12. Telling Java About Exceptions
  14. 12. The Spring Framework
    1. 12.1. Opening Spring-Managed Bean Classes for Extension
    2. 12.2. Persisting Kotlin Data Classes
    3. 12.3. Injecting Dependencies
  15. 13. Coroutines and Structured Concurrency
    1. 13.1. Choosing Coroutine Builders
    2. 13.2. Replacing async/await with withContext
    3. 13.3. Working with Dispatchers
    4. 13.4. Running Coroutines on a Java Thread Pool
    5. 13.5. Cancelling Coroutines
    6. 13.6. Debugging Coroutines
  16. Index

Product information

  • Title: Kotlin Cookbook
  • Author(s): Ken Kousen
  • Release date: November 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492046677