Functions

This part is about functions as values. A function can be stored, passed and returned; a method that takes one is higher-order; a function that keeps a name from where it was written is a closure. The last three lessons are the tools that follow from those three: partial application, partial functions, tail recursion and a parameter that is evaluated only when the body uses it.

Every program of the part is a whole file with its real output, and each one builds on the lesson before it. The higher-order lesson is the one to read first if the rest of the part is a single idea: the other lessons are that idea at work.

3.1 Function values a function stored in a val, its type, and Function1 as the type of one argument Open 3.2 Higher-order functions a method that takes a function, one that returns one, and map, filter and foldLeft Open 3.3 Closures what a function captures, a counter that stays pure, and the value it closes over Open 3.4 Partial application and currying the underscore placeholder, a curried method, curried and tupled Open 3.5 Partial functions isDefinedAt, collect, orElse and lift on a function defined on part of its domain Open 3.6 Tail recursion @tailrec, the accumulator, and the loop a recursive method compiles to Open 3.7 By-name parameters a parameter of type => T, when the argument is evaluated, and a helper that short-circuits Open

Lessons