Apply

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

In mathematics and computer science, Apply is a function that applies functions to arguments. It is central to programming languages derived from lambda calculus, such as LISP and Scheme, and also in functional languages. In particular, it has a role in the study of the denotational semantics of computer programs, because it is a continuous function on complete partial orders.

In category theory, Apply is important in Cartesian closed categories, (and thus, also in Topos theory), where it is a universal morphism, right adjoint to currying.

Programming

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]

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

Common Lisp and Scheme

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):

(apply #'+ (list 1 2))

Similarly in Scheme:

(apply + (list 1 2))

C++

In C++, Bind [2] is used either via the std namespace or via the boost namespace.

C# and Java

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.

variadicFunc(arrayOfArgs);

Go

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

s := []string{"foo", "bar"}
variadicFunc(s...)

Haskell

In Haskell, functions may be applied by simple juxtaposition:

func param1 param2 ...

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.

JavaScript

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:

func.apply(null, args);

Lua

In Lua, apply can be written this way:

function apply(f,...)
  return f(...)
end

Perl

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:

# Equivalent subroutine calls:
@args = (@some_args, @more_args);
func(@args);

func(@some_args, @more_args);

PHP

In PHP, apply is called call_user_func_array:

call_user_func_array('func_name', $args);

Python and Ruby

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:

func(*args)

Python originally had an apply function, but this was deprecated in favour of the asterisk in 2.3 and removed in 3.0.[3]

R

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:

f(x1, x2)
# can also be performed via
do.call(what = f, args = list(x1, x2))

Smalltalk

In Smalltalk, block (function) objects have a valueWithArguments: method which takes an array of arguments:

aBlock valueWithArguments: args

Tcl

Since Tcl 8.5,[4] a function can be applied to arguments with the apply command

apply func ?arg1 arg2 ...?

where the function is a two element list {args body} or a three element list {args body namespace}.

Universal property

Consider a function g:(X\times Y)\to Z, that is, g\isin [(X\times Y)\to Z] where the bracket notation [A\to B] denotes the space of functions from A to B. By means of currying, there is a unique function \mbox{curry}(g) :X\to [Y\to Z] . Then Apply provides the universal morphism

\mbox{Apply}:([Y\to Z]\times Y) \to Z,

so that

\mbox {Apply}(f,y)=f(y)

or, equivalently one has the commuting diagram

\mbox{Apply} \circ \left( \mbox{curry}(g) \times \mbox{id}_Y \right) = g


The notation [A\to B] for the space of functions from A to B occurs more commonly in computer science. In category theory, however, [A\to B] is known as the exponential object, and is written as B^A. There are other common notational differences as well; for example Apply is often called Eval,[5] even though in computer science, these are not the same thing, with eval 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 \lambda, so that \lambda g is written for curry(g). This notation is in conflict with the use of \lambda 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 diagram

Universal property of the exponential object

The articles on exponential object and Cartesian closed category provide a more precise discussion of the category-theoretic formulation of this idea. Thus use of lambda here is not accidental; Cartesian-closed categories provide the general, natural setting for lambda calculus.

Topological properties

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).[6] This property helps establish the foundational validity of the study of the denotational semantics of computer programs.

References

  1. Harold Abelson, Gerald Jay Sussman, Julie Sussman, Structure and Interpretation of Computer Programs, (1996) MIT Press, ISBN 0-262-01153-0. See Section 4.1, The Metacircular Evaluator
  2. http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#with_functions
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. Lua error in package.lua at line 80: module 'strict' not found.
  5. Saunders Mac Lane, Category Theory
  6. H.P. Barendregt, The Lambda Calculus, (1984) North-Holland ISBN 0-444-87508-5