4.12.1.4–4.12.1.5 - Partial application and function composition (C#)

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 square⁡:integer→integer\operatorname{square}:\mathrm{integer}\to\mathrm{integer} returns a value when fully applied: square⁡ 5=25\operatorname{square}\ 5=25. Before application it is a function value; after its only required argument has been supplied, no argument remains.

Ordinary two-argument addition has type integer×integer→integer\mathrm{integer}\times\mathrm{integer}\to\mathrm{integer}. In the successive-argument representation it instead has type

add⁡:integer→(integer→integer).\operatorname{add}:\mathrm{integer}\to(\mathrm{integer}\to\mathrm{integer}).

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 integer→integer→integer\mathrm{integer}\to\mathrm{integer}\to\mathrm{integer} is the same type. Application associates to the left: add⁡ 4 7\operatorname{add}\ 4\ 7 means (add⁡ 4) 7(\operatorname{add}\ 4)\ 7. The arrows describe returned values, not repeated assignments.

A three-argument total can be represented as

total⁡:integer→(integer→(integer→integer)).\operatorname{total}:\mathrm{integer}\to(\mathrm{integer}\to(\mathrm{integer}\to\mathrm{integer})).

After one argument the remaining type is integer→integer→integer\mathrm{integer}\to\mathrm{integer}\to\mathrm{integer}; after two it is integer→integer\mathrm{integer}\to\mathrm{integer}; after three it is an integer result. For total⁡ 2 3 8\operatorname{total}\ 2\ 3\ 8, 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 add⁡:integer→integer→integer\operatorname{add}:\mathrm{integer}\to\mathrm{integer}\to\mathrm{integer}, add⁡ 4\operatorname{add}\ 4 is a function that adds 4, not the value 4. This makes it reusable: (add⁡ 4) 6=10(\operatorname{add}\ 4)\ 6=10, while (add⁡ 4) 9=13(\operatorname{add}\ 4)\ 9=13. 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