Chapter 2. Using functions in Java
This chapter covers
- Understanding functions in the real world
- Representing functions in Java
- Using lambdas
- Working with higher-order functions
- Using curried functions
- Programming with functional interfaces
To understand how functional programming works, we could use functional components provided by some functional library, or even the few that have been made available in the Java 8 library. But instead, we’ll look at how you can construct things rather than how to use these provided components. Once you’ve mastered the concepts, it will be up to you to choose between your own functions and the standard Java 8 ones, or to rely on one of the existing external libraries. In this chapter you’ll create a Function very similar to the Java 8 Function. It will be a bit simplified in how it handles type parameters (avoiding wildcards) in order to make the code easier to read, but it will have some powerful capacities that are absent from the Java 8 version. Apart from those differences, they’ll be interchangeable.
You might have trouble understanding some parts of the code presented in this chapter. That’s to be expected, because it’s very difficult to introduce functions without using other functional constructs such as List, Option, and others. Be patient. All the unexplained components will be discussed in the following chapters.