Programming with functions

 

Chapter 3 from The Joy of Kotlin by Pierre-Yves Saumont

This chapter covers

  • Understanding and representing functions
  • Using lambdas
  • Using higher-order and curried functions
  • Using the right types

In chapter 1, you learned that one of the most important techniques for safer programming is to clearly separate the parts of the programs that don’t depend on anything other than the input data from the part which depends on the state of the outside world. With programs composed of subprograms (called procedures, methods, or functions), this separation transitively applies to these subprograms as well. In Java these are called methods, but in Kotlin they're called functions, which is probably more appropriate because function is a mathematical term with a precise definition (as is usual with mathematics). Kotlin functions can be compared to mathematical functions when they’ve no effect beside returning a value that’s only dependent on their argument. Such functions are often called pure functions by programmers. To write safer programs, therefore, one must

  • Use pure functions for computations
  • Use pure effects for making the result of computations available to the outside world

3.1 What’s a function?

3.1.1 Understanding the relationship between two function sets

3.1.2 An overview of inverse functions in Kotlin

3.1.3 Working with partial functions

3.1.4 Understanding function composition

3.1.5 Using functions of several arguments

3.1.6 Currying functions

3.1.7 Using partially-applied functions

3.1.8 Functions have no effects

3.2 Functions in Kotlin

3.2.1 Understanding functions as data

3.2.2 Understanding data as functions

3.2.3 Using object constructors as functions

3.2.4 Using Kotlin’s fun functions

Exactness

3.2.5 Using object notation versus functional notation

3.2.6 Using value functions

3.2.7 Using function references

3.2.8 Composing functions

3.2.9 Reusing functions