Chapter 1. Lambdas: Parameterizing Code by Behavior
Why Do I Need to Learn About Lambda Expressions?
Over the next two chapters, we’re going to be talking in depth about the relationship between functional and object-oriented programming principles, but first let’s cover some of the basics. We’re going to talk about a couple of the key language features that are related to functional programming: lambda expressions and method references.
Note
If you already have a background in functional programming, then you might want to skip this chapter and move along to the next one.
We’re also going to talk about the change in thinking that they enable which is key to functional thinking: parameterizing code by behavior. It’s this thinking in terms of functions and parameterizing by behavior rather than state which is key to differentiating functional programming from object-oriented programming. Theoretically this is something that we could have done in Java before with anonymous classes, but it was rarely done because they were so bulky and verbose.
We shall also be looking at the syntax of lambda expressions in the Java programming language. As I mentioned in the Introduction, a lot of these ideas go beyond Java; we are just using Java as a lingua-franca: a common language that many developers know well.
The Basics of Lambda Expressions
We will define a lambda expression as a concise way of describing an anonymous function. I appreciate that’s quite a lot to take in at once, so we’re ...