Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain it to me what closure is in real simple language ? [duplicate]

Possible Duplicate:
What are ‘closures’ in .NET?

I am currently looking at lambda expression and the word closure keeps coming. Can someone explain it to me in real simple language.

like image 245
Sandbox Avatar asked Nov 09 '09 11:11

Sandbox


5 Answers

I like the Google example for Javascript (you can morph it for C# easily). It's not something a 5 year old would understand but then I doubt an average 5 year old would understand what a function was.

/*
* When a function is defined in another function and it
*    has access to the outer function's context even after
*    the outer function returns
* An important concept to learn in Javascript
*/

function outerFunction(someNum) {
  var someString = 'Hai!';
  var content = document.getElementById('content');
  function innerFunction() {
    content.innerHTML = someNum + ': ' + someString;
    content = null; // IE memory leak for DOM reference
  }
  innerFunction();
}
like image 200
Chris S Avatar answered Oct 23 '22 02:10

Chris S


I'd say this is a duplicate of: What are ‘closures’ in .NET?

"In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created - i.e. it can still use the local variables etc of the method which created it, even after that method has finished executing."

like image 28
Jorge Córdoba Avatar answered Oct 23 '22 01:10

Jorge Córdoba


Your shoes are in the hall; your jacket is in the kitchen. Put them on, and your gloves (they're in the drawer), when going outside.

Now you can go playing with your cars. At eleven o'clock you must go buy some bread in the corner store.

Kid plays. Forgets all the world.

Alarm clock goes off; kid sees: eleven o'clock! Oh - go outside to buy bread using the "going outside" closure.

like image 12
xtofl Avatar answered Oct 23 '22 02:10

xtofl


The below answer was to the original wording which was akin to "How to explain closures to a 5-year old."

Take this box of legos; build yourself a nice little space craft. When you go to billy's house and bring your space craft there; with closures you can still can use all the pieces in your box of legos, even though the box was left in your bedroom.

like image 8
Alan Avatar answered Oct 23 '22 02:10

Alan


If you really need to keep it simple, then a closure is a function with its context. The function in the closure can still access the same variables it could when it was defined, no matter where you call it from. (In Lua, these are called upvalues, which I think is a very descriptive term.)

I met the concept first in Lua, and this definition helped me understand the concept. Maybe have a look at Lua: its simpleness and power is fascinating, and certainly helps to develop a certain view at other languages. Its concept of closures would be a good example to that.

like image 5
AttishOculus Avatar answered Oct 23 '22 00:10

AttishOculus