4.12.1.1–4.12.1.3 - Function types, first-class values and application (Java)
Describe a function's permitted inputs and outputs, treat functions as values, and distinguish a function from applying it. The mathematical notation describes the functional idea; Java examples show how this route represents it.
Java route status: incomplete: runnable support pending. The source examples and written code questions in this lesson have no Run. These Paper 2 skills do not establish runnable Paper 1 readiness.
Domain, co-domain and function types
A function gives exactly one result for each input in its stated domain.
Function type
means that accepts an argument from and returns a result in . is the domain and is the co-domain. Each is a set of objects from a data type, or a subset of such a set.
For example, let , , and . The function type is . Applying it to every member of produces . Those actual outputs form the range, a subset of the co-domain. The unused values are still members of ; the function need not produce every member of its co-domain. A value such as is outside this particular domain, even though it is an integer.
| Input from | Result in |
|---|---|
Two different inputs may give the same result. For with , both and give . That is still a function. An input being assigned two different results would break the function rule.
In Java, Function<Integer, Integer> describes a function value with Integer input and result types. The first generic type is the argument type; the second is the result type. Generic types use the object type Integer, rather than primitive int. Java's type alone does not express a finite domain such as : the example's contract must also state its allowed values. Mathematical integers are unbounded; Java integer arithmetic has finite limits. All numerical examples here use small values within those limits.
First-class function values
A first-class function can be handled as a value, rather than only called by a fixed name.
First-class value
A first-class value may appear in an expression, be assigned to a variable, be passed as an argument, and be returned as a function-call result. Functions have these capabilities in functional languages and in other languages that support function values.
Integers and strings are familiar first-class values. A function value can be selected and passed around in similar ways. A Java lambda, such as n -> n + 1, supplies the behaviour of a functional interface, an interface with one abstract method. Here Function supplies apply. The lambda expression creates a value implementing that interface; Java does not turn every ordinary method into a standalone function object automatically.
Read this complete Java 8 program. choose returns a function value. Assigning it to rule does not apply that function to an integer. applyTwice receives that value as an argument, and applies it to a value and then to the first result.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account