Filter

From CSE231 Wiki
Revision as of 11:45, 5 February 2020 by Cosgroved (talk | contribs) (Mistakes To Avoid)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, 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 filterWordsWhichContainAllVowels 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

class: FilterUtils.java Java.png
methods: filter
package: filter.studio
source folder: 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.

class: FilterApps.java Java.png
methods: filterEvens
filterWordsWhichContainAllVowels
package: filter.studio
source folder: src/main/java

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

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

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

Useful method: str.indexOf(ch)

Testing Your Solution

Correctness

class: FilterTestSuite.java Junit.png
package: filter.studio
source folder: src/test/java