4.12.1.4–4.12.1.5 - Partial application and function composition (C#)
Learn how supplying some arguments can produce a specialised function, and how composition builds a new function by applying two functions in a specified order.
C# route status: incomplete: runnable support pending. The code questions use written practice.
One-, two- and three-argument types
A multi-argument calculation can be represented as a function that receives one argument and returns a function waiting for the next.
A one-argument function such as returns a value when fully applied: . Before application it is a function value; after its only required argument has been supplied, no argument remains.
Ordinary two-argument addition has type . In the successive-argument representation it instead has type
Applying add to 4 returns an integer-to-integer function. Applying that result to 7 gives 11. Brackets in the type associate to the right, so is the same type. Application associates to the left: means . The arrows describe returned values, not repeated assignments.
A three-argument total can be represented as
After one argument the remaining type is ; after two it is ; after three it is an integer result. For , the stages are a function retaining 2, then a function retaining 2 and 3, then the value 13. A product-domain function and a successive-argument function implement corresponding calculations, but they are not identical types.
Partial application and returned functions
Partial application supplies fewer than all required arguments and produces a function for the arguments still missing.
Partial application
Fixing some arguments of a function to obtain a new function that accepts the remaining arguments. It is different from fully applying the function to obtain its final result.
For , is a function that adds 4, not the value 4. This makes it reusable: , while . A one-argument function whose result is an ordinary value has no non-empty proper subset of arguments to fix: supplying its one argument is full application. A one-argument function can, however, itself return a function, as MakeShift did earlier.
C# does not automatically partially apply an ordinary two-parameter method when one parameter is omitted. Explicit nested functions implement the successive representation:
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account