Function object

Template:Mergefrom

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.

Contents

Description

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 awkward 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 façade for a full object, thus it carries its 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.

Origins

Smalltalk was one of the first languages to support functors through the use of block constructs that are an integral part of the language syntax. For example, one can supply functors as arguments to collection objects to provide filtering & sorting. It is a perfect realization of the strategy pattern that promotes the use of pluggable behaviour.

Functors in C and 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:

/* Declaration of C sorting function */
void sort( int* itemlist, int numitems, int (*cmpfunc)(item*, item*) );
...
/* Callback function */
int compare_function( item* A, item* B )
{
    if( *A < *B ) return -1;
    if( *A > *B ) return +1;
    return 0;
}
...
main()
{
    int items[] = {1, 2, 3, 4};
    sort( items, 4, 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:

class cmp_item // C++ class type functor
{
    int operator()( item* A, item* B );
};
 
int cmp_item :: operator()( item* A, item* B )
{
    if( *A < *B ) return -1;
    if( *A > *B ) return +1;
    return 0;
}
...
// Declaration of C++ sorting function.
template < class cmp > void sort( int * itemlist, int numitems, cmp );
...
main()
{
    int items[] = {1, 2, 3, 4};
    cmp_item cmp;
    sort( items, 4, cmp );
}

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 of 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 a 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.

Take Apache's Jakarta Commons [1] (http://jakarta.apache.org/commons/) project, which provides several common predicate and transformation functors. EqualPredicate, for instance, could be used as follows.

Predicate p = new EqualPredicate ("wikipedia");
if (p.evaluate (args[0]))
  System.err.println ("true; args[0] is 'wikipedia'.");
else
  System.err.println ("false; args[0] is not.");

Notice that p is an object having evaluate method, so it can be treated like every other object.

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 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.

See also

References

External links

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools