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:
publicinterfaceICurriedFunctions{decimalAdd(decimala,decimalb);}varcurry=// some logic for obtaining an implementation of the interfacevaranswer=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:
publicinterfaceICurriedFunctions{decimalAdd(decimala,decimalb);}varcurry=// some logic for obtaining an implementation of the interfacevaranswer=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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access