I've been toying around with Ruby lately and I found myself wondering if in pure object oriented languages (and even those that are not pure) making methods that take only one parameter and then get chained together is equivalent to currying in languages with a functional style? If not, why not? I'd appreciate a detailed, even rigorous answer on the subject.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
Method chaining in object oriented languages is a little different from currying. By definition, the result of currying is a more restricted form of the original function. By convention, the result of method chaining is a modified form of the original (usually non-function) object. Method chaining can be used with unrelated methods on the same class, whereas currying involves returning a function where one or more parameters of the original function are fixed (predetermined).
In Java, method chaining is like:
So, each of those .append() method calls returns a pointer to the anonymous StringBuilder object. This object is complete after each .append(), and it is not a function.
By contrast, in Scala, partial application or currying is like:
(Sample taken from Daniel Yankowsky's blog)
EDIT: Reading this a day later, I think "wrapper-function" is the key. Currying or partial application might best be simulated in Java with wrapper methods.
Method chaining can be similar to partial application or currying, but it can only be equivalent with methods that return other methods (look up Functors), and then the methods need to be set up with return types that meaningfully model currying or partial application.
Method chaining is more often used to implement something like lazy evaluation, as is done with the "Builder" design pattern and the new Collections Library interfaces in Java 8.
I hope that helps.