computer science 209 the strategy pattern ii: emulating higher-order functions

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

Upload: oswin-hood

Post on 25-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

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

Computer Science 209

The Strategy Pattern II:

Emulating Higher-Order Functions

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

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

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

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)93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Strategy pattern with functions!

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

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

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

New Resources

• In the package functools:

– The class HOF, with the static methods map, filter, and reduce

– The interfaces MapStrategy, FilterStrategy, and ReduceStrategy

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

Using the New Resource: Mappingimport functools.HOF;import functools.MapStrategy;

MapStrategy<Integer, Double> sqrt = new MapStrategy<Integer, Double>(){ public Double transform(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

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

Using the New Resource: Filteringimport functools.HOF;import functools.FilterStrategy;

FilterStrategy<Integer> even = new FilterStrategy<Integer>(){ public boolean accept(Integer i){ return i % 2 == 0; }};

List<Integer> list = new ArrayList<Integer>();

List<Integer> results = HOF.filter(even, list);

Obtain the even numbers

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

Using the New Resource: Foldingimport functools.HOF;import functools.ReduceStrategy;

ReduceStrategy<Integer> multiply = new ReduceStrategy<Integer>(){ public Integer combine(Integer i, Integer j){ return i * j; }};

List<Integer> list = new ArrayList<Integer>();

int product = HOF.reduce(multiply, list, 1);

Obtain the product of the numbers

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

The MapStrategy Interfacepackage functools;

public interface MapStrategy<E, R>{

/** * Transforms an element of type E * into a result of type R */ public R transform(E element);

}

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

The FilterStrategy Interfacepackage functools;

public interface FilterStrategy<E>{

/** * Returns true if element is accepted or * false otherwise */ public boolean accept(E element);

}

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

The ReduceStrategy Interfacepackage functools;

public interface ReduceStrategy<E>{

/** * Combines the elements of type E into * a result of type E */ public E combine(E first, E second);}

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

The HOF Classpackage functools;

public class HOF{

public static<E, R> Collection<R> map( MapStrategy<E, R> strategy, Collection<E> col){

// Implementation is an exercise }

public static<E> Collection<E> filter( FilterStrategy<E> strategy, Collection<E> col){

// Implementation is an exercise }…

Implementation uses a for loop, etc.

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

The HOF Classpackage functools;

public class HOF{

… public static<E> E reduce(ReduceStrategy<E> strategy, Collection<E> col, E baseValue){

// Implementation is an exercise }}

Implementation uses a for loop, etc.