Function object: Difference between revisions
m link to operator overloading |
Major rewrite, also provide more examples especially in terms of callback functions. |
||
Line 1: | Line 1: | ||
A '''function object''', often called a '''functor''', is a [[computer programming]] construct allowing an [[object (computer science)|object]] to be invoked or called as if it were an ordinary [[function (programming)|function]], usually with the same syntax. The exact meaning may vary among programming languages. A functor used in this manner in computing bears little relation to the term [[functor]] as used in the mathematical field of [[category theory]]. |
|||
A typical use of a functor is in writing more intelligent [[callback (computer science)|callback functions]]. A callback in [[procedural programming|procedural languages]], such as [[C programming language|C]], may be accomplished by using function pointers. However it can be difficult or akward to pass state into or out of the callback function. This restriction also inhibits more dynamic behavior of the function. A functor solves those problems since the function is really a just a facade for a full object, thus it carries it's own state. |
|||
Recently, in [[C Plus Plus programming language|C++]] and [[Java programming language|Java]], the term '''functor''' was coined, probably independently, with a meaning largely unrelated to [[category theory]]. In [[object-oriented programming|object-oriented software design]] a class can contain both data and function. The term '''functor''' was chosen to describe objects that represent the application of a function to (dynamic) parameter data. |
|||
Most modern object-oriented languages such as [[C Plus Plus programming language|C++]], [[Java programming language|Java]], and [[Python programming language|Python]] support the definition of functors and may even make significant use of them. |
|||
This sort of functor has some of the characteristics that a function pointer would have in [[procedural programming|procedural languages]] such as [[C programming language|C]]. In a procedural program, a sort function might accept a pointer to a "plain" function defining the ordering relation between items to be sorted. In a more object-oriented style, a sort method would accept a functor object that exposes a method defining the ordering. |
|||
== Functors in C++ == |
|||
In Java, the sort method signature would define the functor parameter in terms of a base class (typically an interface). In C++ the sort method would expect the functor class to define an appropriate application [[operator overloading|operator()]]. C++ also allows the possibility that the functor class itself is specified through a template argument. |
|||
Consider the example of a sorting routine which uses a callback function to define an ordering relation between a pair of items. A C program using function pointers may appear as: |
|||
Functors are more powerful than function pointers in that they may contain state (data) that can be used by the function represented. Compilers are sometimes able to generated improved code through [[inlining]]. |
|||
/* C code using function pointers */ |
|||
A functor often contains a single public method apart from the constructor, |
|||
int compare_function( item* A, item* B ) |
|||
although this is not a strict limitation. |
|||
{ |
|||
if( *A < *B ) return -1; |
|||
if( *A > *B ) return +1; |
|||
return 0; |
|||
} |
|||
... |
|||
sort( itemlist, compare_function ); |
|||
In C++ a functor may be used instead of an ordinary function by defining a class which [[operator overloading|overloads]] the function call operator by defining an '''operator()''' member function. In C++ this is called a '''class type functor''', and may appear as follows: |
|||
The C++ [[Standard Template Library]] makes a lot of use of functors, combining the [[functional programming]] paradigm with [[object oriented programming]]. |
|||
// C++ class type functor |
|||
class functor_class { |
|||
int operator( item* A, item* B ) { ... }; |
|||
} |
|||
... |
|||
functor_class X; |
|||
sort( itemlist, X ); |
|||
Notice that the syntax for providing the callback to the sort() function is identical, but an object is passed instead of a function pointer. When invoked the callback function is executed just as any other member function, and therefore has full access to the other members (data or functions) of the object. |
|||
It is possible to use function objects in situations other than as callback functions (although the shortened term ''functor'' is normally not used). Continuing the example, |
|||
functor_class Y; |
|||
int result = Y( a, b ); |
|||
In addition to class type functors, other kinds of function objects are also possible in C++. They can take advantage of C++'s member-pointer or [[generic programming|template]] facilities. The expresiveness of templates allows some [[functional programming]] techniques to be used, such as defining functors in terms of other functors (like [[function composition]]). Much the C++ [[Standard Template Library]] (STL) makes heavy use of template-based function objects. |
|||
== Functors in Java == |
|||
Functors in Java are typically expressed by defining an method signature in a base class (or an interface). Then different functors are created by deriving from the interface. This could be called an inheritance model of functors. |
|||
== Functors in Python == |
|||
A functor in Python is an object that emulates a ''callable object'' (either a plain function or code extensions usually written in C). They are created simply by defining or adding a <tt>__call__()</tt> attribute method in a class or even an individual object. Due to the dynamic nature of the language an ordinary object can be converted into a functor at run-time. |
|||
A special case of a functor is a generator, which is an object that creates a stream of other objects each time it is invoked. |
|||
== Other meanings of functor == |
|||
In some [[functional programming language]]s, such as [[ML programming language|ML]], a functor represents a [[function (mathematics)|mapping]] from modules to modules, and is a technique for reusing code. Functors used in this manner are analogous to the original mathematical meaning of the [[functor]] in [[category theory]]. |
|||
In a more theoretical context a ''function object'' may be considered to be any instance of the class of functions, especially in languages in which functions are [[first-class object]]s. In this case the shortened term functor is rarely used. |
|||
== References == |
|||
* David Vandevoorde, ''C++ Templates: The Complete Guide'', 2003, ISBN 0-201-73484-2.<br/>(Specifically, chapter 22 is entirely devoted to function objects.) |
Revision as of 08:34, 13 August 2004
A function object, often called a functor, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. The exact meaning may vary among programming languages. A functor used in this manner in computing bears little relation to the term functor as used in the mathematical field of category theory.
A typical use of a functor is in writing more intelligent callback functions. A callback in procedural languages, such as C, may be accomplished by using function pointers. However it can be difficult or akward to pass state into or out of the callback function. This restriction also inhibits more dynamic behavior of the function. A functor solves those problems since the function is really a just a facade for a full object, thus it carries it's own state.
Most modern object-oriented languages such as C++, Java, and Python support the definition of functors and may even make significant use of them.
Functors in C++
Consider the example of a sorting routine which uses a callback function to define an ordering relation between a pair of items. A C program using function pointers may appear as:
/* C code using function pointers */ int compare_function( item* A, item* B ) { if( *A < *B ) return -1; if( *A > *B ) return +1; return 0; } ... sort( itemlist, compare_function );
In C++ a functor may be used instead of an ordinary function by defining a class which overloads the function call operator by defining an operator() member function. In C++ this is called a class type functor, and may appear as follows:
// C++ class type functor class functor_class { int operator( item* A, item* B ) { ... }; } ... functor_class X; sort( itemlist, X );
Notice that the syntax for providing the callback to the sort() function is identical, but an object is passed instead of a function pointer. When invoked the callback function is executed just as any other member function, and therefore has full access to the other members (data or functions) of the object.
It is possible to use function objects in situations other than as callback functions (although the shortened term functor is normally not used). Continuing the example,
functor_class Y; int result = Y( a, b );
In addition to class type functors, other kinds of function objects are also possible in C++. They can take advantage of C++'s member-pointer or template facilities. The expresiveness of templates allows some functional programming techniques to be used, such as defining functors in terms of other functors (like function composition). Much the C++ Standard Template Library (STL) makes heavy use of template-based function objects.
Functors in Java
Functors in Java are typically expressed by defining an method signature in a base class (or an interface). Then different functors are created by deriving from the interface. This could be called an inheritance model of functors.
Functors in Python
A functor in Python is an object that emulates a callable object (either a plain function or code extensions usually written in C). They are created simply by defining or adding a __call__() attribute method in a class or even an individual object. Due to the dynamic nature of the language an ordinary object can be converted into a functor at run-time.
A special case of a functor is a generator, which is an object that creates a stream of other objects each time it is invoked.
Other meanings of functor
In some functional programming languages, such as ML, a functor represents a mapping from modules to modules, and is a technique for reusing code. Functors used in this manner are analogous to the original mathematical meaning of the functor in category theory.
In a more theoretical context a function object may be considered to be any instance of the class of functions, especially in languages in which functions are first-class objects. In this case the shortened term functor is rarely used.
References
- David Vandevoorde, C++ Templates: The Complete Guide, 2003, ISBN 0-201-73484-2.
(Specifically, chapter 22 is entirely devoted to function objects.)