To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
Languages
Recent
Show all languages
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Partial application

From Wikipedia, the free encyclopedia

In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments to 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.

Motivation

Intuitively, 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 plus_one. Partial application makes it easy to define these functions, for example by creating a function that represents the addition operator with 1 bound as its first argument.

Implementations

In 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 curry, uncurry and papply to perform currying and partial application explicitly. This might incur a greater run-time overhead due to the creation of additional closures, while Haskell can use more efficient techniques.[1]

Scala implements optional partial application with placeholder, e.g. def add(x: Int, y: Int) = {x+y}; add(1, _: Int) returns an incrementing function. Scala also supports multiple parameter lists as currying, e.g. def add(x: Int)(y: Int) = {x+y}; add(1) _.

Clojure implements partial application using the partial function defined in its core library.[2]

The C++ standard library provides bind(function, args..) to return a function object that is the result of partial application of the given arguments to the given function. Alternatively, lambda expressions can be used:

int f(int a, int b);
auto f_partial = [](int a) { return f(a, 123); };
assert(f_partial(456) == f(456, 123) );

In Java, MethodHandle.bindTo partially applies a function to its first argument.[3] Alternatively, since Java 8, lambdas can be used:

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 assuming method creates a new function with fewer parameters.[4]

The Python standard library module functools includes the partial function, allowing positional and named argument bindings, returning a new function.[5]

In XQuery, an argument placeholder (?) is used for each non-fixed argument in a partial function application.[6]

Definitions

In the simply-typed lambda calculus with function and product types (λ→,×) partial application, currying and uncurrying can be defined as

papply
(((a × b) → c) × a) → (bc) = λ(f, x). λy. f (x, y)
curry
((a × b) → c) → (a → (bc)) = λf. λx. λy. f (x, y)
uncurry
(a → (bc)) → ((a × b) → c) = λf. λ(x, y). f x y

Note that curry papply = curry.

See also

References

  1. ^ Marlow & Peyton Jones 2004
  2. ^ "clojure/clojure, partial function". GitHub. Retrieved 2020-07-18.
  3. ^ "MethodHandle (Java Platform SE 7)". docs.oracle.com. Retrieved 2018-09-12.
  4. ^ "Method assuming". docs.perl6.org. Retrieved 2018-09-12.
  5. ^ "10.2. functools — Higher-order functions and operations on callable objects — Python 3.7.0 documentation". docs.python.org. Retrieved 2018-09-12.
  6. ^ "XQuery 3.1: An XML Query Language". www.w3.org. Retrieved 2018-09-12.

Further reading

External links

This page was last edited on 30 September 2021, at 02:24
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.