Java Reference
In-Depth Information
double gbp = convertUSDtoGBP.applyAsDouble(1000);
As a result, your code is more flexible and it reuses the existing conversion logic! Let's reflect on
what you're doing here. Instead of passing all the arguments x, f, and b all at once to the
converter method, you only ask for the arguments f and b and return another function, which
when given an argument x returns x * f + b. This enables you to reuse the conversion logic and
create different functions with different conversion factors.
Theoretical definition of currying
Currying is a technique where a function f of two arguments (x and y, say) is seen instead as a
function g of one argument that returns a function also of one argument. The value returned by
the latter function is the same as the value of the original function, that is, f(x,y) = (g(x))(y).
Of course, this generalizes: you can curry a six-argument function to first take arguments
numbered 2, 4, and 6 returning a function taking argument 5, which returns a function taking
the remaining arguments, 1 and 3.
When some but fewer than the full complement of arguments have been passed, we often say
the function is partially applied .
Now we turn to another aspect of functional-style programming. Can you really program using
data structures if you're forbidden from modifying them?
14.2. Persistent data structures
In this section, we explore the use of data structures used in functional-style programs. These
come under various names, such as functional data structures and immutable data structures,
but perhaps most common is persistent data structures (unfortunately this terminology clashes
with the notion of persistent in databases, meaning “outliving one run of the program”).
The first thing to note is that a functional-style method isn't allowed to update any global data
structure or any structure passed as a parameter. Why? Because calling it twice is likely to
produce different answers—violating referential transparency and the ability to understand the
method as a simple mapping from arguments to results.
Search WWH ::




Custom Search