3 ms·
Just a bit of a fun post on the subject, I quite like this koan about OO vs Closures [0]: The venerable master Qc Na was walking with his student, Anton. Hopin
by sgrove 10y ago
Just a bit of a fun post on the subject, I quite like this koan about OO vs Closures [0]:
The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures."
Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress.
On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened.
I was surprised at having come to a similar understanding after implementing a very basic OO system in Scheme some years ago.
[0] - http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent http://c2.com/cgi/wiki?ClosuresAndObjectsAreEquivalent
- madmax96 10y agoI was going to post this -- beat me to it! Once you internalize the truth that Objects and Closures are two ways to express the same sort of things, your mind is free to craft wonderful solutions.
- deleted 10y ago[deleted]
- chx 10y agoPHP implements closures as objects and I wrote a blogpost which shows the equivalence at http://drupal4hu.com/node/339.html http://drupal4hu.com/node/339.html .
- Sharlin 10y agoC++ too - a lambda expression (with optional captures) is literally defined in terms of transforming the expression into an instance of a local unnamed class. And of course Java where the equivalence is supremely explicit - before Java 8 anonymous inner classes were the way to implement closures, and the Java 8 lambda expressions are merely syntactic sugar for creating an equivalent AIC instance.
- greydius 10y ago> Java 8 lambda expressions are merely syntactic sugar for creating an equivalent AIC instance I think that's a decent way to explain java's lambda expressions to developers without fp experience, but it's not technically correct. Lambda expressions are compiled into static methods in the class file. /pedantic
- cheez 10y agoHah, I never thought of it that way. In Lisp, you use lambdas to define classes. In C++, you use classes to define lambdas.
- pitay 10y agoAh this reminds me of all the different proposals for closure syntax in Java before it was implemented, the following links put compare some of the proposals: 1. http://www.artima.com/weblogs/viewpost.jsp?thread=180638 http://www.artima.com/weblogs/viewpost.jsp?thread=180638 (Inner Classes or Closures) 2. http://www.artima.com/weblogs/viewpost.jsp?thread=182412 http://www.artima.com/weblogs/viewpost.jsp?thread=182412 (Clear, Consistent, and Concise Syntax (C3S) for Java) 3. https://www.artima.com/weblogs/viewpost.jsp?thread=202004 https://www.artima.com/weblogs/viewpost.jsp?thread=202004 (Comparing Inner Class/Closure Proposals) PS. I remember the Artima articles because I liked them at the time.
- bhntr3 10y agoI was recently working on some ML transformation code. The whole transformer/estimator thing that is pretty standard now in ML libraries. I got really frustrated when I realized that a "pipeline" made of "transformers" is just a composed function where each individual function in the composition is curried/closed over its own parameters. I thought "why don't they just keep this simple and use functions instead of all these cumbersome classes!" Then I started to write something using function composition to keep things simple and got annoyed because sometimes I wanted some helper functions on my functions and it was cumbersome to do that. And I thought "I have made a mistake. The true nature of these things is objects." And then I didn't actually become enlightened. Because everything's an object which is also a closure and we're all made of stardust. But none of that means anyone is going to understand or use my library. Sometimes it's necessary to give things names people expect and keep the higher order abstractions to myself.