Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

closures and objects [closed]

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 programmers rosetta 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 !!

like image 610
vrdhn Avatar asked Feb 01 '09 15:02

vrdhn


People also ask

Are closures objects?

Closures are a poor man's object." At that moment, Anton became enlightened. The point is that objects and closures are two ways of looking at a thing. In a nutshell, objects are data with behaviour, whereas closures are behaviour with data.

What is a closure with example?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.

What are closures in programming?

In programming languages, a closure, also lexical closure or function closure, is a technique for implementing lexically scoped name binding in a language with first-class functions. Operationally, a closure is a record storing a function together with an environment.

Are closures good in programming?

Without closures, you would have to somehow maintain those variables state outside the function, thus littering code outside the function with something that logically belongs inside it.


1 Answers

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

like image 171
bendin Avatar answered Oct 28 '22 12:10

bendin