{"id":476,"date":"2008-05-29T16:00:06","date_gmt":"2008-05-29T23:00:06","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=476"},"modified":"2011-08-07T19:45:38","modified_gmt":"2011-08-08T02:45:38","slug":"lexical-scoping-and-closures","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/lexical-scoping-and-closures\/","title":{"rendered":"Lexical scoping and closures"},"content":{"rendered":"<p>A closure is a just a piece of code with some context - data on which it works. Such things are common in the functional programming community but were left out of the C language because they're actually quite difficult to implement properly.<\/p>\n<p>The C++ language has sufficiently powerful constructs that one can create closures, but the syntax is very unwieldy. One must write:<\/p>\n<pre lang=\"cpp\">class Totalizer\r\n{\r\n    int _total;\r\npublic:\r\n    Totalizer() : _total(0) { }\r\n    void operator(int x) { _total += x; }\r\n    int total() const { return _total; }\r\n};\r\n\r\nint total(Vector<int> x)\r\n{\r\n    Totalizer t;\r\n    for_each(x.begin(), x.end(), t);\r\n    return t.total();\r\n}<\/pre>\n<p>where one would really like to write something like:<\/p>\n<pre lang=\"cpp\">int total(Vector<int> x)\r\n{\r\n    int t;\r\n    for_each (x, lambda(int y) {t += y;} );\r\n    return t;\r\n}<\/pre>\n<p>Here the code is the \"<code>{t += y;}<\/code>\" bit and the context is the \"<code>t<\/code>\" variable, which is local to the \"total\" function.<\/p>\n<p>C# 3.0 provides functionality like this.<\/p>\n<p>What's really going on here is that an object is being created which encapulates some subset of the local variables (the \"<code>t<\/code>\" variable in this case) and which is passed to the <code>for_each<\/code> function (just like in the C++ version). Closures and objects are really just different syntaxes for the same thing.<\/p>\n<p>If we want to return a closure from a function we can do so, but we must then allocate the object in such a way that the \"<code>t<\/code>\" variable will still be valid when the function exits. This is where things get really fiddly. This is one of those things that is easier in a language with garbage collection (you can just allocate the closure on the heap and the GC will take care of cleaning it up when it's finished with) but with care I think it should be possible to do it in a non-GC language as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A closure is a just a piece of code with some context - data on which it works. Such things are common in the functional programming community but were left out of the C language because they're actually quite difficult to implement properly. The C++ language has sufficiently powerful constructs that one can create closures, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-476","post","type-post","status-publish","format-standard","hentry","category-language"],"_links":{"self":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/comments?post=476"}],"version-history":[{"count":1,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/476\/revisions"}],"predecessor-version":[{"id":1318,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/476\/revisions\/1318"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}