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