22+-+Functors - 12/7/2009 First-class Objects What are...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
12/7/2009 1 Functors EECS 280 Programming and Introductory Data Structures 1 First-class Objects What are they? 2 c There are lots of things we can do with integers in a running program, such as: c Create them c Destroy them c Pass them as arguments c Return them as values c In fact, we can do these four things with all values of the simplest types in the language – even types we create ourselves with the enum, struct, or class mechanisms. c For any entity in a programming language, we say that the entity is "first-class" if you can do all four of these things. First-class Objects What they aren’t 3 c Unlike values, types and functions are not first class objects in C++; but, they can sometimes come close. c The template mechanism allows us to pass types as arguments. c The function pointer mechanism allows us to pass functions as arguments and return them as results. c Specifically, we cannot create and destroy functions dynamically. c Sometimes, that's a problem. c Fortunately, C++ gives us a mechanism that is almost exactly like first-class functions, called "function objects". Function Objects Motivation 4 c We'll illustrate this problem by using the list_t ADT from project 2 to write a generic "has-a" function using function pointers. c Remember the operations on a list_t : list_t list_make(int v, list_t l); int list_first(list_t l); list_t list_rest(list_t l); bool list_isEmpty(list_t l); Function Objects Motivation 5 c Given these list operations, suppose we wanted to write the following routine: bool has_odd(list_t l); // EFFECT: returns true if l has // an odd element, false otherwise c The recursive definition of this function is: c The empty list has no odd elements c A non-empty list has odd elements if: c The first element is odd c or the rest of the list has odd elements. Function Objects Motivation 6 c So, the recursive definition of this function is: bool has_odd(list_t l) { if (list_isEmpty(l)) { return false; } else if (list_first(l) % 2) { return true; } else { return has_odd(list_rest(l)); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12/7/2009 2 Function Objects Motivation 7 c If we write has_even , it looks almost literally the same: bool has_even(list_t l) { if (list_isEmpty(l)) { return false; } else if (!(list_first(l) % 2)) { return true; } else { return has_even(list_rest(l)); } } Function Objects Motivation 8 c If we write has_even , it looks almost literally the same: bool has_even(list_t l) { if (list_isEmpty(l)) { return false; } else if (!(list_first(l) % 2)) { return true; } else { return has_even(list_rest(l)); } } We can abstract away the "predicate" as a function and generalize over it. Function Objects
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/26/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.

Page1 / 6

22+-+Functors - 12/7/2009 First-class Objects What are...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online