views:

620

answers:

4

Functional programming .. is like classic ( Mark Twain's type). While reading another articles about SICP, where people are talking about the great impact closures had on there thinking, i got reminded of this, which i read ages ago

Closures are poor man's objects Objects are poor man's closure

( Can't recall exact source but it was programmarers rosatta stone, or zen of programming, or tau of programming ... google has become so crowded that can't go to original source )

So co-programmers ... What is your take ... are closure something that you were always missing .. or just some syntactic sugar, which a preprocessor can do !!

+3  A: 

Closures are much more than syntactic sugar although I think it depends somewhat on your definition of "syntactic sugar".

To me, Java 1.5's for-each was syntactic sugar, but, for example, Ruby blocks go far beyond that. Closures provide a convenient level of abstraction, which is useful for implicit declaration of intent -- a big step toward DSL-ish syntax.

Jeremy Weiskotten
+7  A: 

http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg03277.html

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.

-- Anton van Straaten

bendin
Is that the original source of the quote or just another quoting of it?
AnthonyWJones
The linked message is the original source of this particular koan. It arose in response to a discussion on the closure/object duality, so that notion has been around longer than this particular telling. Follow the link for full details.
bendin
+4  A: 

I've been using closures for a long time in Javascript. Sometimes they would form an 'object', other times they're a useful way to provide instance data to a callback.

As far as I'm concerned they're another useful tool in the box but it goes beyond just sugar. Sugar relates to something you could reasonable do with another somewhat more convoluted syntax. Acheiving closures in a language that doesn't directly support them would be very difficult.

AnthonyWJones
Worse than that, implementing the functionality provided by closures in a language that doesn't directly support them will effectively obfuscate your intentions anywhere it is used. They *are* sugar; but when you're making rock candy you must not discount the importance of sugar...
Shog9
@Shog9: I completely agree with your first sentence. I don't know if I agree or not with the second since I'm finding it difficult to comprehend.
AnthonyWJones
Sorry, i end up cutting too much when i'm limited to 300chars. Rock candy is simply crystallized sugar; if you don't like sugar, there's no point to it. Many high-level programming constructs - closures, objects - are simply tools to communicate with other programmers - sugar. We write rock candy.
Shog9
+2  A: 

Closures and anonymous blocks are pretty important programming constructs and I hit the lack fast in java, C++, C, and about any other language that's missing them. They are especially useful with asynchronous and eventing stuff. They also make function-taking functions more enjoyable to use and provide choices to do more things without additional syntax. (see: smalltalk and scheme)

Cheery
Don't delegates and lambdas of C#c cover it fairly well ?
krosenvold
Delegates seem an interesting feature. I drop the C# from list.
Cheery
@Cherry: why do you drop C# from list?
AnthonyWJones
Because delegates seem to do the same as closures and anonymous blocks. I'm not sure though, this depends about their behavior.
Cheery