Partial applicationIn computer science, partial application (or partial function application) refers to the process of fixing a number of arguments of a function, producing another function of smaller arity. Given a function , we might fix (or 'bind') the first argument, producing a function of type . Evaluation of this function might be represented as . Note that the result of partial function application in this case is a function that takes two arguments. Partial application is sometimes incorrectly called currying, which is a related, but distinct concept. MotivationIntuitively, partial function application says "if you fix the first arguments of the function, you get a function of the remaining arguments". For example, if function div(x,y) = x/y, then div with the parameter x fixed at 1 is another function: div1(y) = div(1,y) = 1/y. This is the same as the function inv that returns the multiplicative inverse of its argument, defined by inv(y) = 1/y. The practical motivation for partial application is that very often the functions obtained by supplying some but not all of the arguments to a function are useful; for example, many languages have a function or operator similar to ImplementationsIn languages such as ML, Haskell and F#, functions are defined in curried form by default. Supplying fewer than the total number of arguments is referred to as partial application. In languages with first-class functions, one can define Scala implements optional partial application with placeholder, e.g. Clojure implements partial application using the The C++ standard library provides int f(int a, int b);
auto f_partial = [](int a) { return f(a, 123); };
assert(f_partial(456) == f(456, 123) );
In Java, public static <A, B, R> Function<B, R> partialApply(BiFunction<A, B, R> biFunc, A value) {
return b -> biFunc.apply(value, b);
}
In Raku, the The Python standard library module In XQuery, an argument placeholder ( DefinitionsIn the simply typed lambda calculus with function and product types (λ→,×) partial application, currying and uncurrying can be defined as
Note that Mathematical formulation and examplesPartial application can be a useful way to define several useful notions in mathematics. Given sets and , and a function , one can define the function where is the set of functions . The image of under this map is . This is the function which sends to . There are often structures on which mean that the image of restricts to some subset of functions , as illustrated in the following examples. Group actionsA group action can be understood as a function . The partial evaluation restricts to the group of bijections from to itself. The group action axioms further ensure is a group homomorphism. Inner-products and canonical map to the dualAn inner-product on a vector space over a field is a map . The partial evaluation provides a canonical map to the dual vector space, . If this is the inner-product of a Hilbert space, the Riesz representation theorem ensures this is an isomorphism. Cross-products and the adjoint map for Lie algebrasThe partial application of the cross product on is . The image of the vector is a linear map such that . The components of can be found to be . This is closely related to the adjoint map for Lie algebras. Lie algebras are equipped with a bracket . The partial application gives a map . The axioms for the bracket ensure this map is a homomorphism of Lie algebras. See also
References
Further reading
External links
|
Portal di Ensiklopedia Dunia