The Joy of Kotlin
Pierre-Yves Saumont
  • MEAP began January 2018
  • Publication in Fall 2018 (estimated)
  • ISBN 9781617295362
  • 475 pages (estimated)
  • printed in black & white
Drudgery like maintaining poor legacy code, interpreting cryptic comments, and writing the same boilerplate over and over can suck the joy out of your life as a Java developer. Fear not! There's hope! Kotlin is an elegant JVM language with modern features and easy integration with Java. The Joy of Kotlin teaches you practical techniques to improve abstraction, design and write comprehensible code, and build maintainable bug-free applications.
Table of Contents detailed table of contents

1. Making programs safer

1.1. Identifying programming traps

1.2. Safely handling effects

1.3. How referential transparency makes programs safer

1.4. The benefits of "safe" programming

1.5. Using the substitution model to reason about programs

1.6. Applying "safe" principles to a simple example

1.7. Pushing abstraction to the limit

1.8. Summary

2. An Overview of Kotlin

2.1. Fields and variables

2.1.1. Omitting the type

2.1.2. Mutable fields

2.2. Classes and interfaces

2.2.1. Making the code even more concise

2.2.2. Implementing an interface or extending a class

2.2.3. Instantiating a class

2.2.4. Overloading constructors

2.2.5. Private constructors

2.2.6. Using accessors

2.2.7. Creating equals and hashcode

2.2.8. Destructuring data objects

2.2.9. Static members

2.2.10. Singletons

2.2.11. Preventing utility class instantiation

2.3. Primitives

2.4. Collections

2.5. Packages

2.6. Visibility

2.6.1. Classes and interfaces

2.7. Functions

2.7.1. Local functions

2.7.2. Overriding functions

2.7.3. Extension functions

2.7.4. Lambdas

2.7.5. Specifying parameter types

2.7.6. Lambdas with multi-line implementation

2.7.7. Using it

2.7.8. Closures

2.8. Handling nulls

2.8.1. Operators for dealing with nullable types

2.8.2. Elvis and the default value

2.9. Control structures

2.9.1. Conditional selectors

2.9.2. Multi conditional selectors

2.9.3. Loops

2.10. Exceptions

2.10.1. Try with resources

2.11. Castings

2.12. Equality vs identity

2.13. String interpolation

2.13.1. Multi-line strings

2.14. Variance

2.14.1. Why is variance a potential problem?

2.14.2. When to use covariance and when to use contravariance

2.14.3. Declaration-site variance vs. use-site variance

2.15. Summary

3. Programming with functions

3.1. What is a function?

3.1.1. What makes a relation between two sets a function

3.1.2. Inverse functions

3.1.3. Partial functions

3.1.4. Function composition

3.1.5. Functions of several arguments

3.1.6. Function currying

3.1.7. Partially applied functions

3.1.8. Functions have no effects

3.2. Functions in Kotlin

3.2.1. Functions as data

3.2.2. Data as functions

3.2.3. Object constructors as functions

3.3. Using fun functions

3.3.1. Object notation versus functional notation

3.4. Value functions

3.5. Using function references

3.6. Composing functions

3.6.1. Exercise 3.1

3.7. Polymorphic functions

3.7.1. Exercise 3.2

3.8. Advanced function features

3.9. What about functions of several arguments?

3.9.1. Exercise 3.3

3.10. Applying curried functions

3.11. Higher-order functions

3.11.1. Exercise 3.4

3.12. Polymorphic higher-order functions

3.12.1. Exercise 3.5 (hard)

3.12.2. Exercise 3.6 (easy now!)

3.13. Using anonymous functions

3.13.1. When to use anonymous and when to use named functions

3.13.2. Type inference

3.14. Local functions

3.15. Closures

3.16. Partial function application and automatic currying

3.16.1. Exercise 3.7 (very easy)

3.16.2. Exercise 3.8

3.16.3. Exercise 3.9 (very easy)

3.16.4. Exercise 3.10

3.16.5. Switching arguments of partially applied functions

3.16.6. Exercise 3.11

3.17. The identity function

3.18. Using the right types

3.18.1. Problems with standard types

3.18.2. Defining value types

3.19. Summary

4. Recursion, corecursion, and memoization

4.1. Understanding corecursion and recursion

4.1.1. Implementing corecursion

4.1.2. Implementing recursion

4.2. How to differentiate recursive and corecursive functions

4.3. Should you chose recursion or corecursion?

4.4. Tail Call Elimination

4.4.1. Using Tail Call Elimination

4.4.2. How to switch from loop to corecursion

4.4.3. Exercise 4.1

4.5. Creating recursive value functions

4.5.1. Exercise 4.2 (hard!)

4.6. Using recursive functions on lists

4.7. Doubly recursive functions

4.7.1. Exercise 4.3

4.8. Abstracting recursion on lists

4.8.1. Exercise 4.4

4.8.2. Exercise 4.5

4.8.3. Exercise 4.6

4.9. Reversing a list

4.9.1. Exercise 4.7

4.9.2. Exercise 4.8

4.10. Building corecursive lists

4.10.1. Exercise 4.9

4.10.2. Exercise 4.10

4.10.3. Exercise 4.11

4.10.4. Exercise 4.12

4.10.5. Exercise 4.13

4.10.6. Exercise 4.14

4.11. The danger of strictness

4.12. Using memoization

4.12.1. Memoization in loop based programming

4.12.2. Memoization in recursive functions

4.12.3. Exercise 4.15

4.12.4. Implicit memoization

4.12.5. Exercise 4.16

4.12.6. Exercise 4.17

4.12.7. Exercise 4.18

4.12.8. Automatic memoization

4.12.9. Memoization of “multi-argument” functions

4.12.10. Are memoized functions pure?

4.13. Summary

5. Data handling with lists

6. Dealing with optional data

7. Safe handling of errors and exceptions

8. Advanced list handling

9. Working with laziness

10. More data handling with trees

11. Solving real problems with advanced trees

12. Safely handling effects

13. Handling and sharing state mutation

14. Solving common problems with safe programming


Appendix A: Property based testing

Appendix B: Mixing Kotlin with Java

About the Technology

The Kotlin programming language offers an expressive syntax, an intuitive type system, and support for a host of tools for you to use. Kotlin runs on the JVM, so it seamlessly integrates with the Java libraries you're already using. Now officially supported by Google as a first-class Android language, there's never been a better time to see what Kotlin can do for you!

About the book

The Joy of Kotlin teaches you the right way to code in Kotlin. In this insight-rich book, you'll master the Kotlin language while exploring coding techniques that will make you a better developer no matter what language you use. Kotlin natively supports a functional style of programming, so seasoned author Pierre-Yves Saumont begins by reviewing the FP principles of immutability, referential transparency, and the separation between functions and effects. Then, you'll move deeper into using Kotlin in the real world, as you learn to handle errors and data properly, encapsulate shared state mutations, and work with laziness. This book will change the way you code—and give you back some of the joy you had when you first started.

What's inside

  • Programming with functions
  • Dealing with optional data
  • Safe handling of errors and exceptions
  • Working with laziness
  • Handling and sharing state mutation

About the reader

Written for intermediate Java or Kotlin developers.

About the author

Pierre-Yves Saumont is an R&D software engineer at Alcatel-Submarine Networks, where he has overseen the previous switch from C to Java, and now to Kotlin too. He has developed several fully functional libraries that are used by business developers. Pierre-Yves is the author of Functional Programming in Java.