See also: Function application. In mathematics and computer science, apply is a function that applies a function to arguments. It is central to programming languages derived from lambda calculus, such as LISP and Scheme, and also in functional languages. It has a role in the study of the denotational semantics of computer programs, because it is a continuous function on complete partial orders. Apply is also a continuous function in homotopy theory, and, indeed underpins the entire theory: it allows a homotopy deformation to be viewed as a continuous path in the space of functions. Likewise, valid mutations (refactorings) of computer programs can be seen as those that are "continuous" in the Scott topology.
The most general setting for apply is in category theory, where it is right adjoint to currying in closed monoidal categories. A special case of this are the Cartesian closed categories, whose internal language is simply typed lambda calculus.
In computer programming, 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.[1] Function application corresponds to beta reduction in lambda calculus.
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 were called with the elements of the list as the arguments. This is important in languages with variadic functions, 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++, Bind [2] is used either via the std namespace or via the boost namespace.
In C# and Java, variadic arguments are simply collected in an array. Caller can explicitly pass in an array in place of the variadic arguments. This can only be done for a variadic parameter. It is not possible to apply an array of arguments to non-variadic parameter without using reflection. An ambiguous case arises should the caller want to pass an array itself as one of the arguments rather than using the array as a list of arguments. In this case, the caller should cast the array to Object
to prevent the compiler from using the apply interpretation.
In Go, typed variadic arguments are simply collected in a slice. The caller can explicitly pass in a slice in place of the variadic arguments, by appending a ...
to the slice argument. This can only be done for a variadic parameter. The caller can not apply an array of arguments to non-variadic parameters, without using reflection..
In Haskell, functions may be applied by simple juxtaposition:
In Haskell, the syntax may also be interpreted that each parameter curries its function in turn. In the above example, "func param1" returns another function accepting one fewer parameters, that is then applied to param2, and so on, until the function has no more parameters.
In JavaScript, function objects have an apply
method, the first argument is the value of the this
keyword inside the function; the second is the list of arguments:
ES6 adds the spread operator func(...args)
[3] which may be used instead of apply
.
In Lua, apply can be written this way:
In Perl, arrays, hashes and expressions are automatically "flattened" into a single list when evaluated in a list context, such as in the argument list of a function
@args = (@some_args, @more_args);func(@args);
func(@some_args, @more_args);
In PHP, apply
is called call_user_func_array
:
In Python and Ruby, the same asterisk notation used in defining variadic functions is used for calling a function on a sequence and array respectively:
Python originally had an apply function, but this was deprecated in favour of the asterisk in 2.3 and removed in 3.0.[4]
In R, do.call
constructs and executes a function call from a name or a function and a list of arguments to be passed to it:
do.call(what = f, args = list(x1, x2))
In Smalltalk, block (function) objects have a valueWithArguments:
method which takes an array of arguments:
Since Tcl 8.5,[5] a function can be applied to arguments with the apply
command
g:(X x Y)\toZ
g\isin[(X x Y)\toZ]
[A\toB]
curry(g):X\to[Y\toZ]
Apply:([Y\toZ] x Y)\toZ
so that
Apply(f,y)=f(y)
or, equivalently one has the commuting diagram
Apply\circ\left(curry(g) x idY\right)=g
More precisely, curry and apply are adjoint functors.
The notation
[A\toB]
[A\toB]
BA
Also, in category theory, curry is commonly denoted by
λ
λg
λ
The articles on exponential object and Cartesian closed category provide a more precise discussion of the category-theoretic formulation of this idea. Thus the use of lambda here is not accidental; the internal language of Cartesian closed categories is simply-typed lambda calculus. The most general possible setting for Apply are the closed monoidal categories, of which the cartesian closed categories are an example. In homological algebra, the adjointness of curry and apply is known as tensor-hom adjunction.
In order theory, in the category of complete partial orders endowed with the Scott topology, both curry and apply are continuous functions (that is, they are Scott continuous).[7] This property helps establish the foundational validity of the study of the denotational semantics of computer programs.
In algebraic geometry and homotopy theory, curry and apply are both continuous functions when the space
YX
X
Y
X