Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.

Slides:



Advertisements
Similar presentations
When do I need an invariant? CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Advertisements

Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Basics of Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under a Creative Commons.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Structural Decomposition CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative Commons.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Patterns of Communication Between Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson 11.1 © Mitchell Wand, This work is licensed under.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.6 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Design Strategies 2: Using a template CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.1 © Mitchell Wand, This work is licensed under a Creative.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Lists of Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Organization of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.2 © Mitchell Wand, This work is licensed under a Creative Commons.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Similar Functions
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
From Templates to Folds
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Case Study: Undefined Variables
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
ormap, andmap, and filter
Generalizing Similar Functions
The Iterative Design Recipe
Rewriting your function using map and foldr
Working with images and scenes
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Presentation transcript:

Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License

Generalization The goal of generalization is to avoid having to repeat code, whether the code is identical or slightly different. In this sequence of lessons, you will learn how to do this, starting with very simple situations, then covering more and more complex situations. 2

Slogans for Generalization Never write the same code twice – Don’t repeat yourself – Single Point of Control fix each bug only once easier maintenance, modification Copy and Paste is bad practice Also known as: Refactoring 3

Module Outline Generalizing a constant to a variable Generalizing over functions Using prepackaged generalizations: map, foldr, etc. A new design strategy: higher-order function composition (HOFC) 4

Generalization Over Constants Over Expressions Over Contexts Over Data Representations Over Method Implementations Mixed Data Data Representations Basics Recursive Data Functional Data Objects & Classes Stateful Objects Design Strategies Function Composition Structural Decomposition Generalization General Recursion Communication via State Module 05 5

Learning Objectives for this Lesson By the end of this lesson, you should be able to – recognize when two functions differ only by a constant – rewrite the two functions using a single more general function – test your new function definitions – use the higher-order function composition strategy 6

Imagine the following: Your boss comes to you and asks you to write a function called find-dog. You follow the design recipe, write the code, and test it. Your boss and you are both happy. Here’s what you wrote: 7

find-dog ;; find-dog : ListOfString -> Boolean ;; GIVEN: a list of strings ;; RETURNS: true iff "dog" is in the given list. ;; STRATEGY: structural decomp. on los : ListOfString (define (find-dog los) (cond [(empty? los) false] [else (or (string=? (first los) "dog") (find-dog (rest los)))])) (check-equal? (find-dog (list "cat" "dog" "weasel")) true) (check-equal? (find-dog (list "cat" "elephant" "weasel")) false) 8

The story continues The next morning, your boss comes to you and asks you to write find-cat. You follow the design recipe, write the code, and test it. Here’s what you wrote: 9

find-cat ;; find-cat : ListOfString -> Boolean ;; GIVEN: a list of strings ;; RETURNS: true iff "cat" is in the given list. ;; STRATEGY: structural decomp. on los : ListOfString (define (find-cat los) (cond [(empty? los) false] [else (or (string=? (first los) "cat") (find-cat (rest los)))])) (check-equal? (find-cat (list "cat" "dog" "weasel")) true) (check-equal? (find-cat (list "elephant" "weasel")) false) 10

A lot of repeated work there! Your boss is happy, but you are less happy; what if the next day, he asks you to write find-elephant ? You feel like you are wasting a lot of time! Let’s see just how alike these functions were. 11

These functions are very similar: (define (find-dog los) (cond [(empty? los) false] [else (or (string=? (first los) "dog") (find-dog (rest los)))])) (define (find-cat los) (cond [(empty? los) false] [else (or (string=? (first los) "cat") (find-cat (rest los)))])) The only differences between the functions are their names, and the fact that one refers to “dog” and the other refers to “cat”. 12

So generalize them by adding an argument ;; find-animal : ListOfString String -> Boolean ;; returns true iff the given string is in the given los. (define (find-animal los str) (cond [(empty? los) false] [else (or (string=? (first los) str) (find-animal (rest los) str))])) (check-expect (find-animal (list "cat" "elephant" "weasel") "elephant") true) (check-expect (find-animal (list "cat" "elephant" "weasel") "beaver") false) Nothing mysterious here! 13

What did we do here? If two functions differ only in a few places, add extra arguments for those places. find-dog and find-cat can be generalized to get find-animal. We replace a constant, like "dog" or "cat" with an argument, here str. Moving common code to a single function with some extra arguments is what is often called "refactoring". 14

Generalization Both functions were special cases of a more general function. The more general function takes extra arguments that express the differences. The arguments "specialize" the function. Must make sure that we can to specialize back to our original functions: 15

Confirm that the original functions can still be expressed. (define (find-dog los) (find-animal los "dog")) (define (find-cat los) (find-animal los "cat")) (define (find-elephant los) (find-animal los "elephant")) find-elephant is now a one-liner. Yay! 16

How to test the new definitions To test the new definitions, comment out the old definitions. This can be accomplished by using the Racket menu item for "comment out with semicolons". An entire parenthesized expression can also be commented out by prefixing it with #; (see the Help Desk for details). Do NOT use the Racket menu item "comment out in a box"—the result will be that your Racket file is converted to a form that is no longer plain text, and will not be viewable with ordinary tools (text editors, web browsers, etc.). 17

Your file should now look like this: #;(define (find-dog los)...) ; (define (find-cat los) ; (cond ;...))) (define (find-animal los str)...) (define (find-dog los) (find-animal los "dog")) The old definitions are commented out find-dog now refers to the new definition 18

Now your old tests should work WITHOUT CHANGE (check-equal? (find-dog (list "cat" "dog" "weasel")) true) (check-equal? (find-dog (list "cat" "elephant" "weasel")) false) (check-equal? (find-cat (list "cat" "dog" "weasel")) true) (check-equal? (find-cat (list "elephant" "weasel")) false) The new definitions of find-dog and find-cat are the only ones visible, so these are now testing the new definitions. 19

Another Example: Pizza! ;; Data Definitions: ;; A Topping is a String. ;; A Pizza is a ListOfTopping ;; INTERP: a pizza is a list of toppings, listed from top to bottom ;; pizza-fn : Pizza -> ?? ;; (define (pizza-fn p) ;; (cond ;; [(empty? p)...] ;; [else (... (first p) ;; (pizza-fn (rest p)))])) ;; Examples: (define plain-pizza empty) (define cheese-pizza (list "cheese")) (define anchovies-cheese-pizza (list "anchovies" "cheese")) The toppings are listed in a certain order, so we must include the order in the interpretation. 20

replace-all-anchovies-with-onions ;; replace-all-anchovies-with-onions ;; : Pizza -> Pizza ;; GIVEN: a pizza ;; RETURNS: a pizza like the given pizza, but with ;; anchovies in place of each layer of onions (define (replace-all-anchovies-with-onions p) (cond [(empty? p) empty] [else (if (string=? (first p) "anchovies") (cons "onions" (replace-all-anchovies-with-onions (rest p))) (cons (first p) (replace-all-anchovies-with-onions (rest p))))])) 21

Opportunities for Generalization We can generalize over onions to get replace- all-anchovies. ;; replace-all-anchovies ;; : Pizza Topping -> Pizza ;; GIVEN: A pizza and a topping ;; RETURNS: a pizza like the given pizza, but ;; with all anchovies replaced by the given ;; topping. 22

Opportunities for Generalization Generalize over anchovies to get replace- topping. ;; replace-topping ;; : Pizza Topping Topping -> Pizza ;; GIVEN: a pizza and two toppings ;; RETURNS: a pizza like the given one, but ;; with all instances of the first topping ;; replaced by the second one. 23

Summary Functions will sometimes differ only in choice of data items. Functions can be generalized by adding new argument(s) for the differences. Confirm the original functions work before generalizing. Test functions by renaming the originals and running the same tests. 24

Next Steps Study 05-1-find-dog.rkt and 05-2-pizza.rkt in the examples folder. If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 5.1 Go on to the next lesson. 25