4.12.1.1–4.12.1.3 - Function types, first-class values and application (Java)

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

f:A→Bf:A\rightarrow B means that ff accepts an argument from AA and returns a result in BB. AA is the domain and BB is the co-domain. Each is a set of objects from a data type, or a subset of such a set.

For example, let A={0,1,2,3}A=\{0,1,2,3\}, B={0,1,2,3,4,5,6}B=\{0,1,2,3,4,5,6\}, and f(x)=2xf(x)=2x. The function type is f:A→Bf:A\rightarrow B. Applying it to every member of AA produces 0,2,4,60,2,4,6. Those actual outputs form the range, a subset of the co-domain. The unused values 1,3,51,3,5 are still members of BB; the function need not produce every member of its co-domain. A value such as 44 is outside this particular domain, even though it is an integer.

Input from AAResult in BB
0000
1122
2244
3366

Two different inputs may give the same result. For h:{−2,−1,0,1,2}→{0,1,2,3,4}h:\{-2,-1,0,1,2\}\rightarrow\{0,1,2,3,4\} with h(x)=x2h(x)=x^2, both −2-2 and 22 give 44. 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 AA: 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