Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Java Functional Programming

You're reading from   Learning Java Functional Programming Create robust and maintainable Java applications using the functional style of programming

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781783558483
Length 296 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Richard M. Reese Richard M. Reese
Author Profile Icon Richard M. Reese
Richard M. Reese
Richard M Reese Richard M Reese
Author Profile Icon Richard M Reese
Richard M Reese
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Getting Started with Functional Programming 2. Putting the Function in Functional Programming FREE CHAPTER 3. Function Composition and Fluent Interfaces 4. Streams and the Evaluation of Expressions 5. Recursion Techniques in Java 8 6. Optional and Monads 7. Supporting Design Patterns Using Functional Programming 8. Refactoring, Debugging, and Testing 9. Bringing It All Together Index

Recursion implementation techniques


There are a few implementation issues that should be addressed. These include the use of wrapper methods and short-circuiting the base case. These techniques can assist in the use of recursion.

Using a wrapper method

A wrapper method is used to support recursion. This method will not actually perform recursion but will call one that does. The wrapper method may:

  • Validate parameters

  • Perform initialization

  • Handle exceptions as errors are generated

The following is an example of a wrapper method for the arrayTotal method, which is duplicated here for your convenience:

    public int arrayTotal(int numbers[], int index) {
        if (index == 0) {
            return numbers[0];
        } else {
            return numbers[index] + arrayTotal(numbers, index - 1);
        }
    }

In the arrayTotalHelper method, the index variable is checked to see if it is within the bounds of the array. If it is not, then the exception needs to be handled:

    public int arrayTotalHelper...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime