Chapter 2. First-Class Functions
Although most functional programming books present immutable variables first, we’re going to start with first-class functions. My hope is that as you read through this chapter, you will see ways in which you could start using some of these ideas at your job tomorrow.
First-class functions are functions treated as objects themselves, meaning we can pass a function as a parameter to another function, return a function from a function, or store a function in a variable. This is one of the most useful features in functional programming, and also one of the most difficult to learn to use effectively.
Introduction to XXY
Welcome to your new company, XXY. You have been hired for your functional programming skills, which your boss would like to use in order to make the company’s code more “functional.” XXY currently uses Java but is interested in some newer languages such as Groovy or Scala. Although you have some ideas, you have been told that the company can’t afford to just “throw away all of its current code and start over.”
All right, it’s time to get down to business. You have been tasked with adding a new function to return a list of enabled customers’ addresses. Your boss tells you the code should be added to the Customer.java file, where XXY is already implementing the same type of functionality (see Example 2-1).