FACTOID # 44: Three quarters of Japanese kids read comics.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Function object

A function object, often called a functor or functionoid, 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 term is a practical usage of the concept of a functor in the mathematical field of category theory. Programming redirects here. ... In strictly mathematical branches of computer science the term object is used in a purely mathematical sense to refer to any thing. While this interpretation is useful in the discussion of abstract theory, it is not concrete enough to serve as a primitive datatype in the discussion of more concrete... In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one, or more, statement blocks; such code is sometimes collected into software libraries. ... Did somebody just say functor? In category theory, a functor is a special type of mapping between categories. ... In mathematics, category theory deals in an abstract way with mathematical structures and relationships between them. ...

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 façade for a full object, thus it carries its own state. A callback is often back on the level of the original caller. ... This does not adequately cite its references or sources. ... C is a general-purpose, block structured, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... A function pointer is a type of pointer in C, C++, D, and other C-like programming languages. ... In computer programming, a facade is an object that provides a simplified interface to a larger body of code, such as a class library. ...


Most modern object-oriented languages such as C++, Java, Python, and Ruby support the definition of functors and may even make significant use of them. C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ... “Java language” redirects here. ... Python is a high-level programming language first released by Guido van Rossum in 1991. ... Ruby is a reflective, dynamic, object-oriented programming language. ...


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. For other uses, see Small talk. ... In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected on-the-fly at runtime. ...


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:

 /* Callback function */ int compare_function(int A, int B) { return (A < B); } ... /* Declaration of C sorting function */ void sort_ints(int* begin_items, int num_items, int (*cmpfunc)(int, int) ); ... int main() { int items[] = {4, 3, 1, 2}; sort_ints(items, sizeof(items)/sizeof(int), 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: In computer programming, operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, = or == have different implementations depending on the types of their arguments. ... This is a list of operators in the C++ and C programming languages. ...

 class compare_class { public: bool operator()(int A, int B) { return (A < B); } }; ... // Declaration of C++ sorting function. template <class ComparisonFunctor> void sort_ints(int* begin_items, int num_items, ComparisonFunctor c); ... int main() { int items[] = {4, 3, 1, 2}; compare_class functor; sort_ints(items, sizeof(items)/sizeof(int), functor); } 

Notice that the syntax for providing the callback to the sort_ints() 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 expressiveness 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. Generic programming is a style of computer programming where algorithms are written in an extended grammar and are made adaptable by specifying variable parts that are then somehow instantiated later by the compiler with respect to the base grammar. ... Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. ... In mathematics, a composite function, formed by the composition of one function on another, represents the application of the former to the result of the application of the latter to the argument of the composite. ... [[Im[[Image:Example. ...


Performance

An advantage of function objects in C++ is performance because unlike a function pointer, a function object can be inlined. For example, consider a simple function which increments its argument implemented as a function object:

 struct IncrementFunctor { void operator()(int&i) { ++i; } }; 

and as a free function:

 void increment_function(int&i) { ++i; } 

Recall the standard library function std::for_each():

 template<typename InputIterator, typename Function> Function for_each(InputIterator first, InputIterator last, Function f) { for ( ; first != last; ++first) f(*first); return f; } 

Suppose we apply std::for_each() like so:

 int A[] = {1, 4, 2, 8, 5, 7}; const int N = sizeof(A) / sizeof(a[0]); for_each(A, A + N, IncrementFunctor()); for_each(A, A + N, increment_function); 

Both calls to for_each() will work as expected. The first call will be to this version:

 IncrementFunctor for_each<int*,IncrementFunctor>(int*, int*, IncrementFunctor) 

the second will be to this version:

 void(*)(int&) for_each<int*,void(*)(int&)>(int*, int*, void(*)(int&)) 

Within for_each<int*,IncrementFunctor>(), the compiler will be able to inline the function object because the function is known at compile time whereas within for_each<int*,void(*)(int&)>() the function cannot be known at compile time and so cannot be inlined.


Actually, a function can easily be known at compile time and the compiler will happily inline it, if it is instructed to. The only requirement is that the compiler has seen the function definition, and that applies equally to functions inside a class or outside. In case we are not inlining however, the linker is instructed to "silently" drop multiple definitions of the same function from different compilation units, without producing an error, but only if said function is a class function. The linker will not dismiss multiple definition of the same function if it is not a class function.


Maintain State

One advantage of functors is that they can maintain state (as fields of the object) between calls. For example, the following code defines a generator (a function that takes no arguments) that counts from 10 up, and we invoke the generator 11 times and print the results. In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. ...

 #include <iostream> #include <iterator> #include <algorithm> class countfrom { private: int count; public: countfrom(int n) : count(n) {} int operator()() { return count++; } }; int main() { std::generate_n(std::ostream_iterator<int>(std::cout, "n"), 11, countfrom(10)); return 0; } 

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. This article or section should be merged with type signature In computer programming, especially object-oriented programming, a method is commonly identified by its unique method signature. ... In computer science, a superclass is a class from which other classes are derived. ... An interface in the Java programming language is an abstract type which is used to specify an interface (in the generic sense of the term) that classes must implement. ...


Take Apache's Jakarta Commons [1] project, which provides several common predicate and transformation functors. EqualPredicate, for instance, could be used as follows. The Jakarta Commons is a subproject of the Jakarta Project under the umbrella of the Apache Software Foundation. ... In mathematics, a predicate is either a relation or the boolean-valued function that amounts to the characteristic function or the indicator function of such a relation. ... ÁInsert non-formatted text here ...

 public class Functors { public static void main(String []args) { 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

In Python, functions are objects, just like strings, numbers, lists, and so on. This feature eliminates the need to create a functor object in many cases. However, any object with a __call__() method may be called using function-call syntax.


An example is this Accumulator class (based on Paul Graham's study on programming language syntax and clarity here): Paul Graham For Paul Graham the photographer, see Paul Graham (photographer). ...

 class Accumulator(object): def __init__(self, n): self.n = n def __call__(self, x): self.n += x return self.n 

An example of this in use (using the interactive interpreter):

 >>> a = Accumulator(4) >>> a(5) 9 >>> a(2) 11 >>> b = Accumulator(42) >>> b(7) 49  

Due to the dynamic nature of the language an ordinary object can be converted into a functor at run-time, but this is rare in practice.


Functors in Lisp

In Common Lisp, Scheme and languages in that family, functions are objects, just like strings, vectors, lists, numbers and so forth. A closure-constructing operator creates a function-object from a piece of the program itself: the piece of code given as an argument to the operator is part of the function, and so is the lexical environment: the bindings of the lexically visible variables are "captured" and stored in the functor, which is more commonly called a closure. The captured bindings play the role of "member variables", and the code part of the closure plays the role of the "anonymous member function", just like operator () in C++. Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard X3. ... Scheme can refer to: The Scheme programming language. ... In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. ...


The closure constructor has the syntax (lambda (parameters ...) code ...). The (parameters ...) part allows an interface to be declared, so that the function takes the declared parameters. The code ... part consists of expressions that are evaluated when the functor is called.


Many uses of functors in languages like C++ are simply emulations of the missing closure constructor. Since the programmer cannot directly construct a closure, he or she must define a class which has all of the necessary state variables, and also a member function. Then, construct an instance of that class instead, ensuring that all the member variables are initialized through its constructor. The values are derived precisely from those local variables that ought to be captured directly by a closure.


A function-object using the class system, no use of closures:

 (defclass counter () ((value :initarg :value :accessor value-of))) (defmethod functor-call ((c counter)) (incf (value-of c))) (defun make-counter (initial-value) (make-instance 'counter :value initial-value)) ;;; use the counter: (defvar *c* (make-counter 10)) (functor-call *c*) --> 11 (functor-call *c*) --> 12 

Since there is no standard way to make funcallable objects in Lisp, we fake it by defining a generic function called FUNCTOR-CALL. This can be specialized for any class whatsoever. The standard FUNCALL function is not generic; it only takes function objects.


It is this FUNCTOR-CALL generic function which gives us functors, which are a computer programming construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. We have almost the same syntax: FUNCTOR-CALL instead of FUNCALL. Some Lisps provide "funcallable" objects as a simple extension. Making objects callable using the same syntax as functions is a fairly trivial business. Making a function call operator work with different kinds of "function things", whether they be class objects or closures is no more complicated than making a + operator that works with different kinds of numbers, such as integers, reals or complex numbers.


Now, a counter implemented using a closure. This is much more brief and direct. The INITIAL-VALUE argument of the MAKE-COUNTER factory function is captured and used directly. It does not have to be copied into some auxiliary class object through a constructor. It is the counter. An auxiliary object is created, but that happens "behind the scenes".

 (defun make-counter (initial-value) (lambda () (incf initial-value))) ;;; use the counter (defvar *c* (make-counter 10)) (funcall *c*) --> 11 (funcall *c*) --> 12 

More than one closure can be created in the same lexical environment. A vector of closures, each implementing a specific kind of operation, can quite faithfully emulate an object that has a set of virtual operations. That type of single dispatch object-oriented programming can be done entirely with closures. In computer science, dynamic dispatch is the process of mapping a method call to a specific sequence of code at runtime (i. ...


So there exists a kind of tunnel being dug from both sides of the proverbial mountain. Programmers in OOP languages discover functors by restricting objects to have one "main" function to "do" that object's functional purpose, and even eliminate its name so that it looks like the object is being called! While programmers who use closures are not surprised that an object is called like a function, they discover that multiple closures sharing the same environment can provide a complete set of abstract operations like a virtual table for single dispatch type OOP. In computer science, dynamic dispatch is the process of mapping a method call to a specific sequence of code at runtime (i. ...


Functors in Ruby

Ruby has a number of objects that can be considered functors, in particular Method and Proc. Ruby also has two kinds of objects that can be thought of as semi-functors: UnboundMethod and block. UnboundMethods must first be bound to an object (thus becoming a Method) before they can be used as a functor. Blocks can be called like functors, but in order to be used in any other capacity as an object (eg. passed as an argument) they must first be converted to a Proc. More recently, symbols (accessed via the literal unary indicator :) can also be converted to Procs. Using Ruby's unary & operator—equivalent to calling to_proc on an object, and assuming that method exists—the Ruby Extensions Project created a simple hack. Ruby is a reflective, dynamic, object-oriented programming language. ... Duck typing is a style of dynamic typing in which an objects current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class. ...

 class Symbol def to_proc proc { |obj, *args| obj.send(self, *args) } end end 

Now, method foo can be a functor, i.e. a Proc, via &:foo and used via takes_a_functor(&:foo). Symbol.to_proc was officially added to Ruby on June 11, 2006 during RubyKaiga2006. [2] is the 162nd day of the year (163rd in leap years) in the Gregorian calendar. ... Year 2006 (MMVI) was a common year starting on Sunday of the Gregorian calendar. ...


Because of the variety of forms, the term Functor is not generally used in Ruby to mean a Function object. Rather it has come to represent a type of dispatch delegation introduced by the Ruby Facets project. The most basic definition of which is: In object-oriented programming there are two notions of delegation. ...

 class Functor def initialize(&func) @func = func end def method_missing(op, *args, &blk) @func.call(op, *args, &blk) end end 

This usage is more akin to that used by functional programming lanaguages, like ML, and the original mathematical terminology.


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, or to the use of templates in C++. Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ... ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM. Historically, ML stands for metalanguage as it was conceived to develop proof tactics in the LCF theorem prover (the language of... Graph of example function, The mathematical concept of a function expresses the intuitive idea of deterministic dependence between two quantities, one of which is viewed as primary (the independent variable, argument of the function, or its input) and the other as secondary (the value of the function, or output). A... Did somebody just say functor? In category theory, a functor is a special type of mapping between categories. ... In mathematics, category theory deals in an abstract way with mathematical structures and relationships between them. ...


In a more theoretical context a function object may be considered to be any instance of the class of functions, especially in languages such as Common Lisp in which functions are first-class objects. In this case the shortened term functor is rarely used. Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard X3. ... In computing, a first-class object (also value, entity, and citizen), in the context of a particular programming language, is an entity which can be used in programs without restriction (when compared to other kinds of objects in the same language). ...


In Prolog and related languages, functor is a synonym for function symbol. Prolog is a logic programming language. ... In formal logic and related branches of mathematics, a functional predicate, or function symbol, is a logical symbol that may be applied to an object term to produce another object term. ...


See also

In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. ... In object-oriented programming, the Command pattern is a design pattern in which objects are used to represent actions. ...

References

  • Vandevoorde, David, & Josuttis, Nicolai M. C++ Templates: The Complete Guide, ISBN 0-201-73484-2
    (Specifically, chapter 22 is entirely devoted to function objects.)

External links


  Results from FactBites:
 
Function Objects (553 words)
In this example, the function object is an object of a user-defined class.
In this example, the function object is of a user-defined class that has local state.
[1] The reason for the name "adaptable function object" is that adaptable function objects may be used by function object adaptors.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m