Pattern Matching
Pattern matching doesnt really appear in mathematics, but assists functional programming in decreasing the need for specific variables. In code we usually encapsulate a group of variables together inside of an object. Pattern matching allows us to better type-check and extract elements from an object, making for simpler and more concise statements with less need for variable definitions.
Functional Programming and Concurrency
Concurrency enables us to do processing in parallel, and we wont cover much about it here because the topic could fill its own book. Some people believe that functional programming actually solves concurrency issues, but this is not actually the case; rather, some of the concepts of functional programming help us to create more well-defined patterns to handle concurrency.
For example, techniques such as message passing help us to create more independent threads by allowing a thread to receive messages without causing another thread to block before it is received.
In addition, features such as immutability help us to define global states and allow global state transitions as a whole rather than partial state changes or major synchronizations between threads.
Conclusion
This chapter was intended as a high-level overview of the important concepts of functional programming. At this point youre probably wondering how can I actually get started using these concepts? As we go through this book, well look at how to implement these features in your code.
In each chapter, Ill introduce a concept, and then well work to refactor and implement it in our example code for the ficticious company XXY. The examples have no driver code. I assume that everyone reading this book can write simple Java main()
functions to test out the code were writing. Im doing this for two reasons.
First, I really want you to write the code and test it out yourself; just reading the examples isnt going to help you understand the concepts or help you become a better functional programmer.
Second, I want to draw attention away from the driver code. Sometimes, when writing more extraneous code to actually call the code that were refactoring, we either forget to refactor the driver code or tend to write too much. Would it really be useful to create a set of driver code to create 10 or 20 Customer
objects?
Each language example is compilable and runnable. The language examples will not rely on external packages. One of the things I truly dislike about some concept books or language books is that normally the author will push you to download third-party packages. In contrast, the goal of this book is to teach you how to deal with functional concepts using the core language.
Chapter 2. First-Class Functions
Although most functional programming books present immutable variables first, were 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 companys 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 cant afford to just throw away all of its current code and start over.
All right, its 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 ).
Example 2-1. Customer.java file contents
import
java.util.ArrayList
;
import
java.util.List
;
public
class
Customer
{
static
public
ArrayList
<
Customer
>
allCustomers
=
new
ArrayList
<
Customer
>();
public
Integer