Higher-order Function Filter Assignment

From CSE231 Wiki
(Redirected from Filter)
Jump to navigation Jump to search

Motivation

In this studio you will be building the first ballot inductee to the Dan Grossman Hall of Fame of Higher Order Functions: filter.

This studio will give us the opportunity to work with the standard Java for-each loop, generics, interfaces, and lambda expressions.

With filter we get to use the for each loop and invoke a method on a generic type interface.

With filterEvens and filterWordsWhichContainAll5Vowels we get to invoke filter with lambda expressions.

Background

Mistakes To Avoid

Attention niels epting.svg Warning: In implementing filter, do NOT mutate the list passed in as a parameter. Return a new list with the items which pass the predicate's test.
Attention niels epting.svg Warning: Do NOT re-implement the filter functionality in filterWordsWhichContainAllVowels and filterEvens. Invoke the filter method you just wrote.

Code to Implement

filter

class: FilterUtils.java Java.png
methods: filter
package: hof.filter.group
source folder: student/src/main/java

method: public static <E> List<E> filter(Predicate<E> predicate, List<E> list) Sequential.svg (sequential implementation only)

This classic high level function returns a list with the elements of list for which predicate's test method returns true. The predicate's test method is applied to each element in order.

clients

class: FilterClients.java Java.png
methods: filterEvens
filterWordsWhichContainAll5Vowels
package: hof.filter.group
source folder: student/src/main/java

We created a couple of utility methods which invoke filter with a custom lambda.

filterWordsWhichContainAll5Vowels

method: public static List<String> filterWordsWhichContainAll5Vowels(List<String> words) Sequential.svg (sequential implementation only)

Filter contains all five vowels.svg

Useful method: str.indexOf(ch)

filterEvens

method: public static List<Integer> filterEvens(List<Integer> xs) Sequential.svg (sequential implementation only)

Filter is even.svg

Testing Your Solution

Correctness

class: _FilterTestSuite.java Junit.png
package: hof.filter.group
source folder: testing/src/test/java