This repository contains code, exercises and solutions from the book "Functional Programming in Java". Along with the book itself, it's the closest you'll get to having your own private functional programming tutor without actually having one.
The code is available as a Gradle project that may be used by:
- Running Gradle commands in a terminal
- Importing in Eclipse (with limitations)
- Importing in IntelliJ
- Importing in NetBeans
One may of course use only the source files to import them into any other editor and/or compile and run them from the command line. Running Gradle commands will not be described here. If you choose this way, you should already know how to do it.
There are two ways to download this project. You may:
- Click on the Download ZIP button on this page (https://github.com/fpinjava/fpinjava) to download a ZIP archive, then unzip it in the directory of your choice
- Or you can clone the project using Git, in order to be able to update it easily when modifications or additions are made to the project. For this, you will need to have Git installed on your PC and use the URL available on this page in the SSH clone URL area.
Note: Eclipse is not fully compatible with Java. Eclipse does not use the Oracle compiler. The compiler used by Eclipse is not fully compatible with the Oracle Java 8 compiler. This might change in the future, but at this time, not all chapter examples will run under Eclipse. You can use Eclipse Luna to edit and run the code through chapter 8. You can't use Eclipse Mars. We have no information about if and when this will be fixed.
To import the project into Eclipse, you must first install the Gradle plug-in. To do this:
- Select Help > Eclipse MarketPlace
- In the Find text area, type Gradle
- Click the Go button
- Select the Gradle integration for Eclipse plugin and click the corresponding Install button.
You will have to accept installing unverified software, and then restarting Eclipse.
You now need to import the fpinjava-parent
project into Eclipse:
- Select File > Import > Gradle > Gradle Project
- In the Import Gradle project dialog box, click the Browse button, navigate to the directory were you put the project and select
the
fpinjava-parent
directory - Click on the Build Model button
- In the dialog box, verify that all modules are selected. If they are not, selecting the parent module will automatically select all sub-modules.
- Keep all standard settings untouched and click Finish. The project should now be imported.
To import the project into intelliJ:
- Select File > Import project
- In the Select file or directory to import dialog box, navigate to the directory where you have installed the project and select
the
fpinjava-parent
directory. - In the import project dialog box, select Gradle and click the Next button.
- Click the Finish button
- Find the
fpinjava-parent
project in the Project pane, right-click the project and select Open module settings. - In the Project Structure dialog box, click on Project in the Project setting pane and select 8 - Lambdas, type annotations etc. for the Project language level option.
- Click OK.
To import the project into Netbeans, you must first install the Gradle plug-in. To do this:
- Select Tools > Plugins > Available Plugins
- In the Search text area, type gradle
- Tick the Install checkbox on the Gradle Support row.
- Click the Install button.
You will have to accept installing unsigned software, and then restarting NetBeans.
You now need to import the fpinjava-parent
project into NetBeans:
- Select File > Open Project...
- In the Open Project dialog box, go to the directory were you put the project and select
the
fpinjava-parent
project name - Click on the Open Project button
The project should now be imported.
For each chapter, you will find two modules called chaptername-exercises
and chaptername-solutions
. Go to the first exercise in
the src/main/java
hierarchy. Here, you will find some code with either a comment saying "To be implemented" or method(s) with the
implementation replaced with a single line throwing a runtime exception. Just implement the missing code.
Note that code is often duplicated from one exercise to the another, so you should not look at the code for exercise 2 before doing exercise 1, since exercise 2 will often contain the solution to exercise one.
To verify that your solution is working, go to the corresponding unit test, in the src/test/java
hierarchy of the same module. Right-click
on the test class (which has the same name as the exercise class with the addition of the Test
suffix) and select Run as JUnit test. The test should
succeed. If it doesn't, fix your code and try again.
If you don't find the correct solution to an exercise, you can look at the corresponding chaptername-solutions
module. You may run the solution
test to verify that the solution is working.
Lots of code is duplicated. This is done so that all exercises are made as independent as possible. However, code reused from previous chapters
is copied to the fpinjava-common
module and should be used from there.
Code modules are generally named after the chapter titles, and not the chapter numbers, which sometimes make them difficult to find. Here is the list of the modules:
-
Chapter 1: fpinjava-introduction
-
Chapter 2: fpinjava-usingfunctions
-
Chapter 3: fpinjava-makingjavafunctional
-
Chapter 4: fpinjava-recursion
-
Chapter 5: fpinjava-lists
-
Chapter 6: fpinjava-optionaldata
-
Chapter 7: fpinjava-handlingerrors
-
Chapter 8: fpinjava-advancedlisthandling
-
Chapter 9: fpinjava-laziness
-
Chapter 10: fpinjava-trees
-
Chapter 11: fpinjava-advancedtrees
-
Chapter 12: fpinjava-state
-
Chapter 13: fpinjava-io
-
Chapter 14: fpinjava-actors
-
Chapter 15: fpinjava-applications
Most modules exist in two versions: exercises and solutions. However, chapters 1 (fpinjava-introduction), 14 (fpinjava-actors) and 15 (fpinjava-applications) have no exercises.
Most modules have unit tests that may be run to verify that your solutions to exercises are correct. However, chapter 13 has not unit tests. Instead, all packages in this chapter have executable programs that may be run to verify that the output corresponds to what is expected.