Chapter 10Advanced Functional Programming

The previous chapter gave you solid foundations to move with agility among Scala's type system. This chapter starts by introducing an advanced type system concept that goes by the name of higher-kinded types.

It proceeds with an overview of the most common functional design patterns, such as functor, applicative and monad. As you'll see, these concepts are much easier than you might think.

The chapter ends with an analysis of two very simple algebraic structures, showing how they can be exploited to write very elegant code.

HIGHER-KINDED TYPES

Option or List are not proper types, but they are kinds. A kind is the type of a type constructor. Simply speaking, it means that, in order to be constructed, it needs another type.

In type theory, kinds like List and Option are indicated using the following symbology: * -> *. The star symbol stands for type. So, * -> * means: “Give me a type and I construct another type.” Indeed, given the List kind, if you provide the Int type then your final type will be List[Int]. If you furnish a String then it'll be a List[String] and so on. Table 10.1 shows the most commonly used kinds.

Table 10.1 Kinds in Type Theory

Symbol Kind Examples
* Simple type. Also known as nullary type constructor or proper ...

Get Professional Scala now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.