Algorithms and OOD (CSC 207 2014F) : Labs

Anonymous Functions


This lab is under development.

Summary: Java 8 introduced anonymous functions (which most people call “lambdas”, even though they don't use the lambdas in the syntax). In this lab, we consider some basic uses of anonymous functions along with predefined interfaces and interfaces you define yourself.

Prerequisite Knowledge: Classes, interfaces, inheritance, polymorphism.

Preparation

a. Review the documentation on the Predicate interface, available at http://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html.

a. Review the documentation on the Function interface, available at http://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html.

b. Fork and clone the repository at https://github.com/Grinnell-CSC207/lab-anonymous-functions.

c. Skim through the various code files in that repository to familiarize yourself with the available functions and their purposes.

Exercises

Exercise 1: Filtered Printing of Integer Vectors

As you've noted, the Utils class has a method, iota, that builds a vector of integers, and a method, printSelectedIntegers, that prints only the elements of a vector of integers that meet a particular criterion. The Even class whose test method takes an integer as a parameter and holds only when that parameter is even.

a. Write a class whose main method builds a moderate-sized vector of integers and prints out only the even values. You can find some boilerplate for that class in Experiment.java.

b. Without creating a new class or using anonymous functions, update your main method so that it prints out only the odd values.

c. Using an anonymous procedure, update your main method so that it prints out only the values which are multiples of 5.

Exercise 2: Generalized Filtered Printing

a. The Utils.printSelectedIntegers procedure works only with vectors of integers and integer predicates. It makes sense to generalize the code. Write a static method, Utils.printSelected, that takes as input a vector of values of type T and a predicate for T values and prints out only the values for which the predicate holds.

b. Use Utils.printSelected to replicate the actions from the previous exercise.

c. Write a program that uses printSelected to print the elements of the following array that have five or fewer letters.

    String[] tmp = 
        new String[] { "alpha", "bravo", "charlie", "delta", "echo",
                       "foxtrot", "golf", "hotel", "india",
                       "juliett", "kilo", "lima", "mike", 
                       "november", "oscar", "papa", "quebec",
                       "romeo", "sierra", "tango", "uniform",
                       "victor", "whiskey", "xray", "yankee", "zulu" };
    Vector<String> strings = new Vector<String>(Arrays.asList(tmp));

d. Write a program that uses printSelected to print the elements of that array that include the letter “o”.

e. Write a program that uses printSelected to print all of the elements of the array.

Exercise 3: Selecting Values

a. Write a static generic method, select(Vector<T> vals, Predicate<T>), that creates a new vector that contains only the elements of vals for which the predicate holds.

b. Write a program to select the values in strings (from the previous problem) whose length is at least five.

c. Write a program to select the values in strings which contain two vowels in a row.

d. Write a program to select the values in iota(50) that are odd.

Exercise 4: Removing Values

Rather than building a new vector by selecting values, as in the previous problem, we could also write a method that mutates an existing vector by removing values.

a. Write a static generic method, remove(Vector<T> vals, Predicate<T>), that removes from vals any values for which the predicate holds.

b. Extend your program to remove the values in strings whose length is at least five.

c. Extend your program to instead remove the values in strings which contain two vowels in a row.

d. Extend your program to remove the values in iota(50) that are multiples of 2. Then remove the values in the result that are multiples of 3. Then remove the values in the result that are multiples of 5. What, if anything, do you observe about the remaining values?

Exercise 5: Map

a. Implement a static generic method, map(Function<T,R> fun, Vector<T> vec) that builds a new Vector<R> by applying fun to each element of vec.

b. Using map, build a new version of strings in which every string is converted to uppercase.

c. Using map, build a new veresion of strings in which the first letter in each string is converted to uppercase.

For Those With Extra Time

Think about how you might use the remove method to implement the Sieve of Eratosthenes.

If you still have extra time, go back and finish the lab on generics.

If you still have extra time, read about anonymous inner classes.