Except where otherwise noted, the contents of this document are Copyright 2019 Stuart Reges and Marty Stepp.
Goals for this problem set:
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.
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]; } }
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
lambdaSquare
Write a lambda expression that converts an integer into the square of that integer. For example, 4 would become 16.
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"
.
stream: A sequence of elements from a data source that supports aggregate operations.
Common stream operations:
map
: Apply a given operation to each element of the stream.
filter
: Retain or remove each element from the stream based on a boolean test.
reduce
: Combine the elements in some way into a single result.
// 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();
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
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.
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.
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.
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.