- Apply
In
mathematics andcomputer science , Apply is a function that applies functions to arguments. It is a central concept inprogramming language s derived fromlambda calculus , such asLISP and Scheme, and also infunctional language s. In particular, it has a role in the study of thedenotational semantics of computer programs, by virtue of the fact that it is a continuous function oncomplete partial order s.In
category theory , Apply is important inCartesian closed categories , (and thus, also inTopos theory ), where it is auniversal morphism ,right adjoint tocurrying .Programming
In computer programing, apply applies a function to a list of arguments. "
Eval " and "apply" are the two interdependent components of the "eval-apply cycle", which is the essence of evaluating Lisp, described in SICP. [ [http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-26.html The Metacircular Evaluator] (SICP Section 4.1)]Apply function
Apply is also the name of a special function in many languages, which takes a function and a list, and uses the list as the function's own argument list, as if the function was called with the elements of the list as the arguments. This is important in languages with
variadic function s, because this is the only way to call a function with an indeterminate (at compile time) number of arguments.In
Common Lisp apply is a function that applies a function to a list of arguments (note here that "+" is a variadic function that takes any number of arguments):Similarly in Scheme:In C# and Java, variadic arguments are simply collected in an array; you can explicitly pass in an array in place of the variadic arguments:InJavaScript , function objects have anapply
method, the first argument is the value of thethis
keyword inside the function; the second is the list of arguments:InPerl , lists and hashes are automatically "flattened" when inserted into a list environment, such as an argument list of a function:InPHP ,apply
is calledcall_user_func_array
:In Python and Ruby, you use the same asterisk notation used in defining variadic functions to call a function on a sequence:Universal property
Consider a function , that is, where the bracket notation denotes the
space of functions from "A" to "B". By means ofcurrying , there is a unique function . Then Apply provides theuniversal morphism :,
so that
:
or, equivalently one has the
commuting diagram :
The notation for the space of functions from "A" to "B" occurs more commonly in computer science. In
category theory , however, is known as theexponential object , and is written as . There are other common notational differences as well; for example "Apply" is often called "Eval" [Saunders Mac Lane, "Category Theory"] , even though in computer science, these are not the same thing, witheval distinguished from "Apply", as being the evaluation of the quoted string form of a function with its arguments, rather than the application of a function to some arguments.Also, in category theory, "curry" is commonly denoted by , so that is written for "curry"("g"). This notation is in conflict with the use of in
lambda calculus , where lambda is used to denote free variables. With all of these notational changes accounted for, the adjointness of "Apply" and "curry" is then expressed in the commuting diagramThe articles on
exponential object andCartesian closed category provide a more precise discussion of the category-theoretic formulation of this idea. Thus use of lambda here is not accidental; Cartesian close categories provide the general, natural setting for lambda calculus.Topological properties
In
order theory , in the category ofcomplete partial order s endowed with theScott topology , both "curry" and "apply" arecontinuous function s (that is, they areScott continuous ). [H.P. Barendregt, "The Lambda Calculus", (1984) North-Holland ISBN 0-444-87508-5] This property helps establish the foundational validity of the study of thedenotational semantics of computer programs.References
Wikimedia Foundation. 2010.