Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Strategy Pattern II: Emulating Higher-Order Functions

Similar presentations


Presentation on theme: "The Strategy Pattern II: Emulating Higher-Order Functions"— Presentation transcript:

1 The Strategy Pattern II: Emulating Higher-Order Functions
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions

2 Higher-Order Functions
In functional or procedural languages, a higher-order function allows clients to apply a function parameter to a sequence of values map: transform the values filter: retain the values that pass a test reduce: boil or fold the values down to a single value

3 Examples of HOFs in Python
>>> list(map(math.sqrt, [2, 4, 6])) [ , 2.0, ] >>> list(filter(lambda n: n % 2 == 0, range(1, 11))) [2, 4, 6, 8, 10] >>> functools.reduce(lambda x, y: x * y, range(1, 101), 1) Strategy pattern with functions!

4 Map/Filter/Reduce in Java
Java has no functions, only methods Think of an HOF as a static method, with a list argument and an object argument that implements a particular kind of strategy Define a strategy interface for each type of HOF Will work with lists, but could generalize to any collections or iterable objects

5 New Resources In the package functools:
The class HOF, with the static methods map, filter, and reduce The interfaces Function, Predicate, and BiFunction

6 The Function Interface
package functools; public interface Function<T, R>{ /** * Transforms an element of type T * into a result of type R the transformed element */ public R apply(T element); } This strategy is used in the map function

7 The Predicate Interface
package functools; public interface Predicate<T>{ /** true if element passes a test or * false otherwise */ public boolean test(T element); } This strategy is used in the filter function

8 The BiFunction Interface
package functools; public interface BiFunction<T, R>{ /** * Combines the elements of type T and R into * a result of type R the combined element */ public R apply(R first, T second); } This strategy is used in the reduce function

9 Using the New Resource: Mapping
import functools.HOF; import functools.Function; Function<Integer, Double> sqrt = new Function<Integer, Double>(){ public Double apply(Integer i){ return Math.sqrt(i); } }; List<Integer> list = new ArrayList<Integer>(); List<Double> results = HOF.map(sqrt, list); Obtain the square roots of a bunch of integers

10 Using the New Resource: Filtering
import functools.HOF; import functools.Predicate; Predicate<Integer> even = new Predicate<Integer>(){ public boolean test(Integer i){ return i % 2 == 0; } }; List<Integer> list = new ArrayList<Integer>(); List<Integer> results = HOF.filter(even, list); Obtain the even numbers

11 Using the New Resource: Folding
import functools.HOF; import functools.BiFunction; BiFunction<Integer, Integer> multiply = new BiFunction<Integer, Integer>(){ public Integer apply(Integer i, Integer j){ return i * j; } }; List<Integer> list = new ArrayList<Integer>(); int product = HOF.reduce(1, multiply, list); Obtain the product of the numbers

12 The HOF Class Implementation uses a for loop, etc. package functools;
public class HOF{ public static<T, R> List<R> map(Function<T, R> strategy, List<T> list){ // Implementation is an exercise } public static<T> List<T> filter(Predicate<T> strategy, Implementation uses a for loop, etc.

13 The HOF Class Implementation uses a for loop, etc. package functools;
public class HOF{ public static<T, R> R reduce(R baseValue BiFunction<T, R> strategy, List<T> list){ // Implementation is an exercise } Implementation uses a for loop, etc.

14 Creating a Strategy Class
import functools.HOF; import functools.Function; Function<Integer, Double> sqrt = new Function<Integer, Double>(){ public Double apply(Integer i){ return Math.sqrt(i); } }; List<Integer> list = new ArrayList<Integer>(); List<Double> results = HOF.map(sqrt, list); Obtain the square roots of a bunch of integers

15 Just Passing a Method Obtain the square roots of a bunch of integers
import functools.HOF; import functools.Function; List<Integer> list = new ArrayList<Integer>(); List<Double> results = HOF.map((Integer i) -> Math.sqrt(i), list); Obtain the square roots of a bunch of integers


Download ppt "The Strategy Pattern II: Emulating Higher-Order Functions"

Similar presentations


Ads by Google