Functional Programming
Alessandro Lacava, Janek Bogucki, Aliaksandr Bedrytski, Matthew de Detrich, Benjamin Neil · 2016
This chapter discusses the classical pillars of functional programming language and explains how they can be implemented using Scala. Programming languages have a different approach to immutability: some of them embrace it and do not allow any mutable state, and others allow it only through constants that, arguably, are semantically different from a simple immutable value. Scala allows both approaches through val and var keywords. The chapter covers higher order functions that helps with writing more meaningful and declarative code applied to collections. Partially applied functions may be useful in conjunction with pattern matching to avoid useless repetitions. The chapter examines two useful features of functional programming: the Option type and lazy execution. The former provides a way to describe a function that may have an absent value just by its return type, while the latter is a nice way to bypass value initialization until later when it's needed.