Closures are interesting and helpful. In short, closure is a function that can be exposed to the outside world that captures part of its external environment, even though the environment could be private to outside world.
This puzzle involves following short snippet inspired by the Javascript: Definitive Guide book
// create array of 10 functions
static Func<int>[] constfuncs()
{
Func<int>[] funcs = new Func<int>[10];
for ( var i = 0; i < 10; i++ )
{
funcs[i] = () => i;
}
return funcs;
}
...
var funcs = constfuncs();
for ( int i = 0; i < 10; i++ )
Console.WriteLine( funcs[i]() );
// output:
// 10
// 10
// ...
// 10
Side note: closures in Javascript work very similar and the output of corresponding Javascript snippet would be the same
function constfuncs() {
var funcs = [];
for(var i = 0; i < 10; i++)
{
funcs[i] = function() { return i; };
}
return funcs;
}
var funcs = constfuncs();
for ( var i = 0; i < 10; i++ )
console.log( funcs[i]() );
End of side note
Your task here is not only to explain the behavior (easy part) but also correct the inner loop of the constfuncs method so that the code outputs
0
1
2
3
4
5
6
7
8
9
More specifically, you are allowed to modify only this line of code
funcs[i] = () => i;
You are free to propose a solution for the JavaScript version as well. The book doesn’t provide one, if you are interested.