Chapter 8. Currying and Partial Application
Currying and partial application are two more functional concepts that come straight out of old math papers. The former has absolutely nothing to do with Indian food, delicious though it is;1 in fact, it’s named after the preeminent American mathematician Haskell Brooks Curry, after whom no fewer than three programming languages are named.2
As noted in Chapter 1, currying came from Curry’s work on combinatory logic, which served as a basis for modern FP. Rather than give a dry, formal definition, I’ll explain by example. This is a bit of vaguely C#-like pseudocode for an Add()
function:
public
interface
ICurriedFunctions
{
decimal
Add
(
decimal
a
,
decimal
b
);
}
var
curry
=
// some logic for obtaining an implementation of the interface
var
answer
=
curry
.
Add
(
100
,
200
);
In this example, we’d expect the answer to simply be 300 (i.e., 100 + 200), which is indeed what it would be.
What if we were to provide only a single parameter, however? Like this:
public
interface
ICurriedFunctions
{
decimal
Add
(
decimal
a
,
decimal
b
);
}
var
curry
=
// some logic for obtaining an implementation of the interface
var
answer
=
curry
.
Add
(
100
);
// What could it be?
In this scenario, if this were a hypothetical curried function, what do you think you’d have returned to you in answer
?
I’ve devised a rule of thumb when working in FP, as I mentioned in Chapter 1—if there’s a question, the answer is likely to be “functions.” And that’s the case here.
If this were ...
Get Functional Programming with C# now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.