JavaScript.
function closure(init) {
var cnt = init;
return function() {
return ++cnt;
}
}
var r = closure(10);
alert(r());
alert(r());
alert(r());
C#.
Func<int, Func<int>> closure = init =>
{
int cnt = init;
return () => ++cnt;
};
var r = closure(10);
Console.WriteLine(r());
Console.WriteLine(r());
Console.WriteLine(r());
Output.
11
12
13
No comments:
Post a Comment