24+-+Functors - 9/1/10 First-class Objects What are they?...

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

View Full Document Right Arrow Icon
9/1/10 1 Functors EECS 280 Programming and Introductory Data Structures 1 First-class Objects What are they? 2 There are lots of things we can do with integers in a running program, such as: Create them Destroy them Pass them as arguments Return them as values 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. 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 Unlike values, types and functions are not first class objects in C+ +; but, they can sometimes come close. The template mechanism allows us to pass types as arguments. The function pointer mechanism allows us to pass functions as arguments and return them as results. Specifically, we cannot create and destroy functions dynamically. Sometimes, that's a problem. Fortunately, C++ gives us a mechanism that is almost exactly like first-class functions, called "function objects". Function Objects Motivation 4 We'll illustrate this problem by using the list_t ADT from project 2 to write a generic "has-a" function using function pointers. 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 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 The recursive definition of this function is: The empty list has no odd elements A non-empty list has odd elements if: The first element is odd or the rest of the list has odd elements. Function Objects Motivation 6 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
9/1/10 2 Function Objects Motivation 7 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 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.

Page1 / 6

24+-+Functors - 9/1/10 First-class Objects What are they?...

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