Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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 list of values and receive a list of results –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])) [1.4142135623730951, 2.0, 2.449489742783178] >>> 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) 9332621544394415268169923885626670049071596826438162146859 2963895217599993229915608941463976156518286253697920827223 758251185210916864000000000000000000000000 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 collection argument and an object argument that implements a particular kind of strategy Define a strategy interface for each type of HOF Should work with any iterable collection

5 New Resources In the package functools : –The class HOF, with the static methods map, filter, and reduce –The interfaces MapStrategy, FilterStrategy, and ReduceStrategy

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

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

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

9 The MapStrategy Interface package functools; public interface MapStrategy { /** * Transforms an element of type E * into a result of type R */ public R transform(E element); }

10 The FilterStrategy Interface package functools; public interface FilterStrategy { /** * Returns true if element is accepted or * false otherwise */ public boolean accept(E element); }

11 The ReduceStrategy Interface package functools; public interface ReduceStrategy { /** * Combines the elements of type E into * a result of type E */ public E combine(E first, E second); }

12 The HOF Class package functools; public class HOF{ public static Collection map( MapStrategy strategy, Collection col){ // Implementation is an exercise } public static Collection filter( FilterStrategy strategy, Collection col){ // Implementation is an exercise } … Implementation uses a for loop, etc.

13 The HOF Class package functools; public class HOF{ … public static E reduce(ReduceStrategy strategy, Collection col, E baseValue){ // Implementation is an exercise } Implementation uses a for loop, etc.


Download ppt "Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions."

Similar presentations


Ads by Google