Map, filter and reduce Programming by composition.

Slides:



Advertisements
Similar presentations
Talk tonight Richard Stallman Norton 112 6:00 PM.
Advertisements

How do visitors work? This set of slides traces through the execution of a visitor we’ve seen before (the lengthVisitor) on a short list. It shows how.
Lists We’ve seen an array-based list implementation, the ArrayList. Advantage of an array-based implementation: –fast access to a specific index –typically.
1 Computing Functions with Turing Machines. 2 A function Domain: Result Region: has:
Lists We’ve seen an array-based list implementation, the ArrayList.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Function: Definition A function is a correspondence from a first set, called the domain, to a second set, called the range, such that each element in the.
2-1 Relations and Functions
Temperature Readings The equation to convert the temperature from degrees Fahrenheit to degrees Celsius is: c(x) = (x - 32) The equation to convert the.
Relations and Functions
Functions. Warm Up Solve each equation. 1.2x – 6 = x = X + 29 = x – 5 – 4x = 17 x = 14 x = - 7 x = -15 x = 11.
Temperature Readings The equation to convert the temperature from degrees Fahrenheit to degrees Celsius is: c(x) = (x - 32) The equation to convert the.
1.2 Represent Functions as Rules and Tables EQ: How do I represent functions as rules and tables??
Objectives 1. To determine if a relation is a function.
+ Represent Relations and Functions. + Relation A relation is a mapping, or pairing, of input values with output values. The set of input values in the.
Functions Section 1.4. Relation The value of one variable is related to the value of a second variable A correspondence between two sets If x and y are.
Relations and Functions By: Jeffrey Bivin Lake Zurich High School Last Updated: November 14, 2007.
Math – What is a Function? 1. 2 input output function.
How do we verify and find inverses of functions?
Do Now Find the domain & range:. Answers to Homework
Mathematical Preliminaries
Relations and Functions Intermediate Algebra II Section 2.1.
Bell Ringer 10/30/ Objectives The student will be able to: 1. identify the domain and range of a relation. 2. show relations as sets and mappings.
SOLUTION EXAMPLE 1 Represent relations Consider the relation given by the ordered pair (–2, –3), (–1, 1), (1, 3), (2, –2), and (3, 1). a. Identify the.
Temperature Readings The equation to convert the temperature from degrees Fahrenheit to degrees Celsius is: c(x) = (x - 32) The equation to convert the.
FUNCTIONS FUNCTIONS DOMAIN: THE INPUT VALUES FOR A RELATION. USUALLY X INDEPENDENT VARIABLE RANGE: THE OUTPUT VALUES FOR A RELATION. USUALLY.
I CAN DETERMINE WHETHER A RELATION IS A FUNCTION AND I CAN FIND DOMAIN AND RANGE AND USE FUNCTION NOTATION. 4.6 Formalizing Relations and Functions.
State the domain and range of each relation. Unit 3, Lesson 2 Mrs. King.
Recursion Higher Order Functions CSCE 314 Spring 2016.
Goal: Identify and graph functions..  Relation: mapping or pairing, of input values with output values.  Domain: Set of input values.  Range: set of.
Review Functions. Function A function is a special type of relation in which each element of the domain is paired with exactly one element of the range.
Algebra 2 June 18, 2016 Goals:   Identify functions in coordinate, table, or graph form   Determine domain and range of given functions.
1 Computing Functions with Turing Machines. 2 A function Domain Result Region has:
Chapter 8.1 vocabulary Relation Is a pairing of numbers or a set of ordered pair {(2,1) (3,5) (6, 3)} Domain: first set of numbers Range: Second set of.
Functions & Graphing.
Computing Functions with Turing Machines
2-1 Relations and Functions
Relations and Functions
2-1 Relations and Functions
EXAMPLE 1 Represent relations
7.4 Functions Designed by Skip Tyler.
Hash table another data structure for implementing a map or a set
Notes Over 2.1 Function {- 3, - 1, 1, 2 } { 0, 2, 5 }
2.1 – Represent Relations and Functions.
Objective 1A f(x) = 2x + 3 What is the Range of the function
Relations and Functions
Functions Introduction.
Computing Functions with Turing Machines
Function Notation “f of x” Input = x Output = f(x) = y.
Recitation Outline C++ STL associative containers Examples
Function notation.
Activity 2.8 Study Time.
Functions.
2.1: Represent Relations and Functions HW: p.76 (4-20 even, all)
FUNCTIONS.
MATH 1310 Section 3.6.
Objectives The student will be able to:
2.1: Relations and Functions
Functions and Relations
Unit 3 Day 4.
MATH 1310 Section 3.6.
Objectives The student will be able to:
Lesson 5.3 What is a Function?
Objectives The student will be able to:
Relation (a set of ordered pairs)
I can determine whether a relation is a function
Objectives The student will be able to:
2-1 Relations & Functions
Chapter 2 Functions, Equations, and Graphs
Presentation transcript:

Map, filter and reduce Programming by composition

Mapping Apply operation to each element of a list, gathering the results in a new list. Example: – list: (“Your” “call” “is” “important” “to” “us”) – operation: string length – result: ( )

Conceptual diagram (specific case) (“Your” “call” “is” “important” “to” “us”) MAP String length ( )

Conceptual diagram (generalized) Input list MAP Unary Operation Output list

Conceptual diagram (generalized) Input list MAP Unary Operation Output list INVARIANT

VARIANTS Conceptual diagram (generalized) Input list MAP Unary Operation Output list

public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Code (Map)

public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Code (Map) Invariant parts

public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Code (Map) Invariant parts

public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Map operation to rest of list, getting answer recursively Code (Map) Invariant parts

public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Apply operation to first item of list Code (Map) Invariant parts

public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Insert result into new list Code (Map) Invariant parts

public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } Code (Map) Variant parts VARIANT: the input list

public class Map implements IAlgo,Domain,LRStruct > { public LRStruct emptyCase(LRStruct host, IUnaryOperation arg) { return new LRStruct (); } public LRStruct nonEmptyCase(LRStruct host, IUnaryOperation arg) { return host.getRest().execute(this,arg).insertFront(arg.apply(host.getDatum())); } VARIANT: the operation Code (Map) Variant parts VARIANT: the operation

The IUnaryOperation interface public interface IUnaryOperation { public Range apply(Domain arg); }

Mapping: Domain  Range f: x  f(x) DOMAIN RANGE x f(x)

A concrete operation public class StringLen implements IUnaryOperation { public Integer apply(String arg) { return arg.length(); } DOMAI N RANGE

A concrete operation public class Times2 implements IUnaryOperation { public Integer apply(Integer arg) { return 2*arg; } public String toString() { return "x -> 2*x"; } DOMAI N RANGE

Reduction Combine all values of a list using (op,id) a binary operation op, with identity element id. Example: – list: ( ) – operation (+,0) – result: 23

The IBinaryOperation interface public interface IBinaryOperation { public Range apply(Domain1 arg1, Domain2 arg2); public Range identityElement(); }

Filtering Apply a predicate to each element of a list, building a new list of those elements for which the predicate is true. Example: – list: (“Your” “call” “is” “important” “to” “us”) – predicate: contains exactly one vowel – result: (“call” “is” “to” “us”)

The IPredicate interface public interface IBinaryOperation { public Range apply(Domain1 arg1, Domain2 arg2); public Range identityElement(); }