Building Java Programs

Lab: Functional Programming

Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.

Lab goals

Goals for this problem set:

Functional Programming

lambda

functional programming: Emphasizes the use of functions (methods) to decompose a complex task into subtasks.

Key components:

side effect: A change to the state of a program or object caused by a call on a function.

Exercise : sideEffectDoubleAll

What side effect does the following function have? How could it be rewritten to avoid side effects?

// Doubles the values of all elements in an array.
public static void doubleAll(int[] a) {
    for (int i = 0; i < a.length; i++) {
        a[i] = 2 * a[i];
    }
}

Lambdas

lambda: An expression that describes a function by specifying its parameters and the value that it returns. Syntax:

(parameters) -> expression

Example: a lambda that accepts an integer and returns that integer plus 1:

(x) -> x + 1

Exercise : lambdaSquare

Write a lambda expression that converts an integer into the square of that integer. For example, 4 would become 16.

Exercise : lambdaLastFirst

Write a lambda expression that accepts two strings representing a first and last name and concatenates them together into a string in "Last, First" format. For example, if passed "Cynthia" and "Lee", it would return "Lee, Cynthia".

Streams

stream: A sequence of elements from a data source that supports aggregate operations.

Common stream operations:

// Example: sum of squares of odd integers in a stream
int sum = IntStream.of(3, 1, 4, 1, 5, 9, 2, 6, 5, 3)
    .filter(n -> n % 2 != 0)
    .map(n -> n * n)
    .sum();

Exercise : largestEven

Write a method largestEven that uses stream operations to find and return the largest even number from an array of integers. For example, if the array is {5, -1, 12, 10, 29, 2, 8}, your method should return 12. You may assume that the array contains at least one even integer.

You must use stream operations to solve this problem. Do not use any loops or recursion.

API reference: Arrays.stream, IntStream

Exercise : countNegatives

Write a method countNegatives that uses stream operations to count how many numbers in a given array of integers are negative. For example, if the array is {5, -1, -3, 20, 47, -10, -8, -4, 0, -6, -6}, return 7.

You must use stream operations to solve this problem. Do not use any loops or recursion.

Exercise : printDoubled

Write a method printDoubled that uses stream operations to print twice the value of each element of array of integers. For example, if the array passed is {2, -1, 4, 16}, print:

4
-2
8
32

You must use stream operations to solve this problem. Do not use any loops or recursion.

Exercise : pigLatin

Write a method pigLatin that uses stream operations to convert a string parameter into its "Pig Latin" form. For this problem we'll use a simple definition of Pig Latin where the first letter should be moved to the end of the word and followed by "ay." For example, if the string passed is "go seattle mariners", return "o-gay eattle-say ariners-may".

You must use stream operations to solve this problem. Do not use any loops or recursion.

Exercise : fourLetterWords

Write a method fourLetterWords that accepts a file name as a parameter and uses stream operations to return a count of the number of unique lines in the file that are exactly four letters long. Assume that each line in the file contains a single word.

You must use stream operations to solve this problem. Do not use any loops or recursion.

API reference: Files, Paths