4.12.1.4–4.12.1.5 - Partial application and function composition (Java)
Use one-, two- and three-argument function types to recognise when an application returns a value or another function. Then build new functions by partial application and composition, preserving the order and types in the functional notation.
Java route status: incomplete: runnable support pending. Java source tasks here are written, have no Run, and support Paper 2 functional reasoning.
One-, two- and three-argument types
The arrows in a function type tell you what remains after each argument is supplied.
A one-argument rule, , returns an integer when its integer argument is supplied. Thus . Supplying its only argument is full application; there is no remaining input to fix later.
A two-input rule can be modelled using a pair, . A curried form expresses it as a succession of one-argument functions:
The first application returns a function. The next application returns the integer result. Function-type arrows associate to the right, but successive application associates to the left: means . It first obtains the add-4 function, then applies it to , giving . It does not mean applying addition to the result of an application of 4.
For a three-input rule , its curried type is
| Expression | Type of the result | Meaning |
|---|---|---|
| Three inputs remain | ||
| Fix ; two inputs remain | ||
| Fix ; one input remains | ||
| Fully apply the function; result |
The right-associated type describes returned functions, rather than one function with a three-element tuple argument. Both representations can express the same numerical rule, but their intermediate results have different types.
Partial application and returned functions
Partial application fixes some arguments and produces a new function waiting for the others.
Partial application
Applying fewer than all the required arguments specialises a function. Its result is a function of the remaining arguments, not the final scalar result. Currying provides a convenient representation for this process.
For , the result is the rule , of type . Applying it to gives . Creating the add-4 function can be useful even if no second argument is available yet. For the three-input cost rule, produces . Applying that function to gives .
Java does not automatically curry BiFunction: add.apply(4) is invalid because its apply requires two arguments. We represent the returned function explicitly with nested Function types and lambdas. In the following complete program, a -> b -> a + b first receives a and returns the lambda b -> a + b. That returned lambda retains the supplied value of a.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account