The Strategy Pattern II: Emulating Higher-Order Functions

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Methods Java 5.1 A quick overview of methods
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
CS18000: Problem Solving and Object-Oriented Programming.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Eight compound procedures and higher-order procedures.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
Scala Overview Brandon Clopton CSCI 431. Scala fuses object-oriented and functional programming in a statically typed programming language. It is aimed.
Information and Computer Sciences University of Hawaii, Manoa
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Computer Science 209 Software Development Inheritance and Composition.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Computer Science 112 Fundamentals of Programming II.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
160 as a product of its prime factors is 2 5 x 5 Use this information to show that 160 has 12 factors.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Functional Processing of Collections (Advanced) 6.0.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Fundamentals of Programming I Higher-Order Functions
Functional Programming
CS314 – Section 5 Recitation 10
Higher Order Functions
EECE 310: Software Engineering
Introduction to Higher Order (Functional Programming) (Python) part 2
Computer Programming Fundamentals
Functional Programming and Stream API
Arrays 2/4 By Pius Nyaanga.
Recitation 11 Lambdas added to Java 8.
CMPT 201 Functions.
Functional Programming
Java Algorithms.
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
6.001 SICP Data abstractions
null, true, and false are also reserved.
Chapter 9 Web Services: JAX-RPC, WSDL, XML Schema, and SOAP
Command Line Arguments
PPL Sequence Interface.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Object Oriented Programming in java
Chapter 7 The Java Array Object © Rick Mercer.
Building Java Programs Chapter 19
Building Java Programs Chapter 19
Computer Science 111 Fundamentals of Programming I
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
2/24/2019.
CSE 3302 Programming Languages
Computer Science 312 Composing Functions Streams in Java 1.
Containers and Iterators
Recursion notes Chapter 8.
Computer Science 312 Lambdas in Java 1.
Computer Science 312 What’s New in Java 8? 1.
Today’s topics Abstractions Procedural Data
slides created by Alyssa Harding
ormap, andmap, and filter
Introduction to the Lab
List Interface ArrayList class implements the List Interface
functions are also data! other functions as input!
Java’s Stream API.
Introduction to Computer Science
Presentation transcript:

The Strategy Pattern II: Emulating Higher-Order Functions 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 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

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!

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

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

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

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

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

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

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

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

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.

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.

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

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