Computer Science 209 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.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
CS18000: Problem Solving and Object-Oriented Programming.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Computer Science 209 Software Development Iterators.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Eight compound procedures and higher-order procedures.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Java Unit 9: Arrays Declaring and Processing Arrays.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Lambdas and Streams. Functional interfaces Functional interfaces are also known as single abstract method (SAM) interfaces. Package java.util.function.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Information and Computer Sciences University of Hawaii, Manoa
Types in programming languages1 What are types, and why do we need them?
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
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.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
160 as a product of its prime factors is 2 5 x 5 Use this information to show that 160 has 12 factors.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
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.
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
Fundamentals of Programming I Higher-Order Functions
CS314 – Section 5 Recitation 10
Higher Order Functions
EECE 310: Software Engineering
Introduction to Higher Order (Functional Programming) (Python) part 2
Lecture 7: Android Services
Arrays 2/4 By Pius Nyaanga.
Software Development Iterators
Software Development Inheritance and Composition
CMPT 201 Functions.
The Strategy Pattern II: Emulating Higher-Order Functions
6.001 SICP Data abstractions
null, true, and false are also reserved.
Command Line Arguments
Iterator.
Object-Oriented Programming Paradigm
Object Oriented Programming in java
Chapter 7 The Java Array Object © Rick Mercer.
Computer Science 111 Fundamentals of Programming I
2/24/2019.
Computer Science 312 What’s New in Java 8? 1.
Introduction to Computer Science
Introduction to the Lab
functions are also data! other functions as input!
A type is a collection of values
Introduction to Computer Science
Presentation transcript:

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

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!

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

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

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

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

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

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); }

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

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); }

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.

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.