<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx</link><description>JScript, as I noted yesterday, is a functional language. That doesn't mean that it works particularly well (though I hope it does) but rather that it treats functions as first-class objects. Functions can be passed around and assigned to variables just</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>RE: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#53029</link><pubDate>Thu, 18 Sep 2003 00:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53029</guid><dc:creator>Blake</dc:creator><description>For the record, the 'closures are you friend' comment was meant to be humor and not a blanket recommendation.

One unexpected place I have found JScript's closures particularly useful is in breaking up long calculations that have a lot of state on the stack.  You can't pump messages manually in the browser but you can window.setTimeout(function() { /* next block of code */ }, 0) which permits you progress bar to repaint.  No memory is leaked in this scenario.

Obviously this is not the sort of use of closures one sees in a more traditionally functional language, but it solves an otherwise intractable problem.</description></item><item><title>RE: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#53030</link><pubDate>Thu, 18 Sep 2003 22:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53030</guid><dc:creator>Adrian</dc:creator><description>I'm confused...  I thought that the definition of &amp;quot;functional programming&amp;quot; was that everything is accomplished by application of functions to existing values, thus producing new values -- the most obvious feature of such a language being the lack of &amp;quot;destructive&amp;quot; operators.  The definition at the &amp;quot;Functional Programming FAQ&amp;quot; (see http://www.cs.nott.ac.uk/~gmh//faq.html#functional-languages) seems to agree.

But Eric defines a functional language as one that has functions as first-class objects.  This seems like a totally different issue.  In fact, it seems like first-class functions aren't too rare in languages that wouldn't usually be considered &amp;quot;functional.&amp;quot;

Am I missing something?</description></item><item><title>RE: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#53031</link><pubDate>Thu, 18 Sep 2003 22:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53031</guid><dc:creator>Eric Lippert</dc:creator><description>Look at it this way: functional programming is as you define it.  A &amp;quot;functional language&amp;quot; is a language in which one can do functional programming.  

Therefore JScript is a functional language.  It is also a procedural language and an object oriented language, as it supports those programming styles as well.

The fact that JScript allows you to do non-functional programming doesn't, by the definition above, make it not a functional language.  After all, you can write non-pure-functional programs in Scheme too.

Does that make more sense?</description></item><item><title>RE: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#53032</link><pubDate>Fri, 19 Sep 2003 22:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53032</guid><dc:creator>elias</dc:creator><description> It is not clear what to call functional programming.
 Scheme may be considered an imperative language, because it allows assignment and other imperative features like i/o &amp;quot;functions&amp;quot;. On the other hand languages like Miranda, or Haskell are pure functional languages, because of the lack of states (introduced by assignment, and imperative procedures)  semantics is clear.  More over those languages are lazy, it means they do not perform a computation if it is not needed, it is an advantage see the next pseudocode

  f(x,y) = if x &amp;gt; 5 then x else y
  g(x,y) = f(x,x/y)
  h = g(10,0)

What is the value of h? 10 of course, try this in Scheme or Java or any eager (non lazy) language, and you get a division by 0 error exception (another imperative feature)  the reason is that 10/0 is never computed in a lazy language, the if-then-else function is lazy in many languages (test?thenpart:elsepart in C).

Other important feature in functional languages is that you can build programs just by function composition.
Not by applications, it is you do not need variables.

  h = filter (&amp;gt;5) . map (^2)
  h [1..10]
computes:
  [9,16,25,36,49,64,81,100]

 [1..10] is the list [1,2,3,4,5,6,7,8,9,10] 
may be defined (in a lisp style) as
 filter test list = if (null list) then [ ] 
                     else if test (hd list) 
                            then (hd list):filter test (tl list)
                            else filter test (tl list)
 but functional languages allow pattern matching in definitions it is shorter and more clear
   filter test [ ] = [ ]
   filter test (x : xs) = if (test x) then x : filter test xs 
                            else test xs
just write a case for every constructor (for lists [], and : (cons) ).  and map is defined by:
   map f [ ] = [ ]
   map f (x : xs) = f x : map f xs

(&amp;gt;5) in Lisp/Scheme you would write (lambda (x)(&amp;gt; x 5))
and (lambda(x)(^ x 2)) instead of (^2), you may have noticed the absence of parethesis in function arguments
 map f list  instead of  map (f, list) the reason is that functions are in curried form (curried in honor of Haskell Brooks Curry, founder of combinatory logic) it means they take one argument at a time, for that reason
(1 +) denotes a function expecting a number calculating its sucessor.  

the dot is functional composition you use it in mathematics.

  ( f . g ) x = f ( g x )

it is possible, but not practical, to never use variables, more over you can build every function with just 2 elementary funtions  S and K 
defined as follows K x y = x  and S x y z = x  y ( x z )
combining SKK you get I the identity function I x = x
this is of theoretical interest, but also practical interest, 
Erik said  &amp;quot;closures are your friends&amp;quot;, well one may say 
&amp;quot;thunks are your friends&amp;quot; instead,  thunks are closure like structures used to implement lazy evaluation, the other way to imlement it is by transforming the funtion into elementary functions or combinators SKI would be enough but David Turner (also the author of Miranda) 
developed a better set known as Turner's combinators.

I can't see the lenght of this comment, but I think it is large enough, so my conclusion:

Higher Order Functions in Java/JavaScript is a good thing.  Although they use closures not thunks, but is ok for a non lazy language.  The lack of lists in imperative languages (you can define your data type i know)  may limit its use because problems with stack overflow due to parameter passing semantics on those languages and the lack of efficient garbage collector.
The lack of curried functions (although you may define a curried version with higher order functions)  do not encourage to think programs as function composition, it encourage the applicative style (Scheme has the same limitant (I am a radical pure functional programer)).

You want to write functional programs, then it would be better to embed a functional language in browsers, otherwise the patch to imperative laguages wont be easy to use.  However is a nice feature to have higher order functions, no doubt.  But not enough.




  





</description></item><item><title>RE: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#53033</link><pubDate>Sun, 21 Sep 2003 22:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53033</guid><dc:creator>Dan Shappir</dc:creator><description>Given the content of this discussion,I think it's appropriate to mention the BeyondJS JavaScript library developed by Sjoerd Visscher and myself. You can find it at http://w3future.com/html/beyondJS/ (a slightly dated version, I will upload a newer version any day now).

Not your standard run-of-the-mill JavaScript library, BeyondJS targets the JavaScript language itself. Building on JavaScript's intrinsic functional capabilities it adds loads of features that transform it into a truly functional programming language. Not a pure functional language but a functional language non-the-less.

Here are some of the features BeyondJS provides:
1. currying
2. functional composition
3. list comprehensions
4. lazy lists (lists whose members are lazy evaluated)
5. streams
a lots more.

For example, lets implement the example provided by elias using JavaScript with BeyondJS:

(1).to(10).collect(&amp;quot;^&amp;quot;.curry(2)).filter(&amp;quot;&amp;lt;&amp;quot;.curry(5));

Actually, (1).to(10) computes the JavaScript Array [1,2,3,4,5,6,7,8,9,10]. If you want a lazy list you would use (1).lazy(10) instead.

Speaking of lazy lists, BeyondJS even supports recursive list definitions.  Consider this Haskell definition Fibonacci sequence:

fibs = 0 : 1 : (zipWith (+) fibs (tail fibs))

Using BeyondJS you get the slightly more verbose:

var fibs = (0).lazy(1);
fibs.extend(fibs.zipWith(&amp;quot;+&amp;quot;, fibs.tail());

Summing the integers from 1 to 100:

var sum = (1).lazy(100).fold(&amp;quot;+&amp;quot;);

A nice thing about BeyondJS is that it works with IE5+ (most of the stuff works in IE4 as well), with Mozilla and with Rhino.

Here is another cool example which works both using WSH and Rhino:

File.StdIn.collect(function(line) { return line.split(&amp;quot;,&amp;quot;).reverse(); }).feed(File.StdOut);

This code reads coma delimited data from the standard input and outputs it to the standard output with the column order reversed (note: requires a version of BeyondJS not yet online). Because File.StdIn is a lazy sequence of the input lines, the data is never wholly stored in memory.

Suppose I only want to retain lines the contain the word 'good', regardless of case:

File.StdIn.filter(/good/i).collect(function(line) { return line.split(&amp;quot;,&amp;quot;).reverse(); }).feed(File.StdOut);

Note that while JavaScript is obviously not a pure functional language, you can use BeyondJS to write fully functional programs (pun intended) without any variables.</description></item><item><title>RE: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#53034</link><pubDate>Wed, 24 Sep 2003 13:17:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:53034</guid><dc:creator>Xy</dc:creator><description>The function literal is very handy when defining neat Objects.

function Car() {
    this.broom = function(loud) {
    }
}</description></item><item><title>Daily links</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#427325</link><pubDate>Thu, 09 Jun 2005 19:51:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:427325</guid><dc:creator>Worm in liquid maze</dc:creator><description>General&lt;br&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://www.jnd.org/dn.pubs.html&amp;amp;amp;nbsp;-"&gt;http://www.jnd.org/dn.pubs.html&amp;amp;amp;nbsp;-&lt;/a&gt; a collection of essays on various topics from design...</description></item><item><title>&amp;Xi; bigXi &amp;raquo; IE memory leak, revisited</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#563399</link><pubDate>Tue, 28 Mar 2006 23:44:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:563399</guid><dc:creator>Ξ bigXi » IE memory leak, revisited</dc:creator><description>PingBack from &lt;a rel="nofollow" target="_new" href="https://bigxi.wordpress.com/2006/03/28/ie-memory-leak-revisited/"&gt;https://bigxi.wordpress.com/2006/03/28/ie-memory-leak-revisited/&lt;/a&gt;</description></item><item><title>Understanding and Solving Internet Explorer Leak Patterns</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#2010595</link><pubDate>Mon, 02 Apr 2007 11:21:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2010595</guid><dc:creator>adminadmin</dc:creator><description>&lt;p&gt;The Evolution of the Web Developer In the past, memory leaks haven't posed huge problems for Web developers. Pages were kept relatively simple and navigation between different locations within a site was a great way to clean up any loose memory. If there..&lt;/p&gt;
</description></item><item><title>Fabulous Adventures In Coding &amp;laquo; [REF]</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#3028958</link><pubDate>Fri, 01 Jun 2007 22:11:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3028958</guid><dc:creator>Fabulous Adventures In Coding « [REF]</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://amref.wordpress.com/2007/06/01/fabulous-adventures-in-coding/"&gt;http://amref.wordpress.com/2007/06/01/fabulous-adventures-in-coding/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Night Dreaming (by Sudar) &amp;raquo; Is FireFox 1.0.4 unstable?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#4511370</link><pubDate>Wed, 22 Aug 2007 17:42:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4511370</guid><dc:creator>Night Dreaming (by Sudar) » Is FireFox 1.0.4 unstable?</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://sudarmuthu.com/blog/2005/06/23/is-firefox-104-unstable.html"&gt;http://sudarmuthu.com/blog/2005/06/23/is-firefox-104-unstable.html&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Recursion, Part Four: Continuation Passing Style</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#4796111</link><pubDate>Fri, 07 Sep 2007 01:58:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4796111</guid><dc:creator>Fabulous Adventures In Coding</dc:creator><description>&lt;p&gt;We're getting hung up on the stack management aspects of recursive programming. Why do we need a stack&lt;/p&gt;
</description></item><item><title>[转载]理解并解决IE的内存泄漏方式[翻译]</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#4939384</link><pubDate>Sun, 16 Sep 2007 12:27:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4939384</guid><dc:creator>H-bomb</dc:creator><description>&lt;p&gt;在接下来的内容中，我们会讨论内存泄露方式，并为每种方式给出示例。其中一个重要的示例是JScript中的Closure技术，另一个示例是在事件执行中使用Closures。当你熟悉本示例后，你就能找出并修改你已有的大多数内存泄漏问题，但是其它Closure相关的问题可能又会被忽视。&lt;/p&gt;
</description></item><item><title>re: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#4985847</link><pubDate>Tue, 18 Sep 2007 23:30:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4985847</guid><dc:creator>gheorghe</dc:creator><description>&lt;p&gt;Closers are a wonderful concept that works very well on all other languages that have it, even server side JavaScript. Too bad the implementers of JavaScript from browser makers have decided that garbage collection is done on two layers, one from page DOM and one from the JavaScript engine, so from what could be a great tool in making better code in a real functional way, we have to write procedural code because the language implementers messed up.&lt;/p&gt;
</description></item><item><title>Consciousness &amp;raquo; Scheme Programming Guides</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#7906168</link><pubDate>Tue, 26 Feb 2008 19:00:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7906168</guid><dc:creator>Consciousness » Scheme Programming Guides</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://journal.suteki.nu/2008/02/26/scheme-programming-guides/"&gt;http://journal.suteki.nu/2008/02/26/scheme-programming-guides/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>(转)理解并解决IE的内存泄漏方式(二)</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#8384703</link><pubDate>Sat, 12 Apr 2008 12:58:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8384703</guid><dc:creator>孙钰佳</dc:creator><description>&lt;p&gt;不得不说Eric Lippert同志疾呼的：Don't use closures unless you really need closure semantics. In most cases, non-nested functions are the right way to go. 是非常消极的应付之辞。今天关于IE内存泄漏的文章已有很多，而且很大部分就是微软的自己人在解释，甚至象Eric Lippert这样引擎开发组的成员。但是他们的文章始终没有正面承认其实这就是IE的bug，而且是非常严重的bug，这事情其实完全不关脚本引擎对象和DOM对象的事。就是微软对产品不负责任的表现，不说IE4，1997那个春天那是太遥远了点，但是IE6也是2001年随xp发布的。使用COM可以给他们的开发带来很多便利，当然也利用很多现成的东西，可是居然在带来这样的严重问题后，他们却把大部分责任归咎于不合理和不正确的使用Closures技术！对于循环引用产生Memory..&lt;/p&gt;
</description></item><item><title>  ??????Javascript?????????????????????(???) - SILENCE SPACE - ???????????????</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#8497675</link><pubDate>Tue, 13 May 2008 02:52:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8497675</guid><dc:creator>  ??????Javascript?????????????????????(???) - SILENCE SPACE - ???????????????</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://czsilence.yo2.cn/articles/javascript-memory-leak-4.html"&gt;http://czsilence.yo2.cn/articles/javascript-memory-leak-4.html&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#8589989</link><pubDate>Tue, 10 Jun 2008 17:52:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8589989</guid><dc:creator>freelancer</dc:creator><description>&lt;p&gt;mootools / prototype libraries heavily use closures, seems to be working ok on IE.&lt;/p&gt;
</description></item><item><title>re: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#9021268</link><pubDate>Wed, 29 Oct 2008 03:37:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9021268</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;Isn't this a bit head in the sand? Rather than fix your memory leak problems, you ask people not to use a fundamental and powerful part of JavaScript (or the hacked JScript). No other browsers seem to suffer in this way.&lt;/p&gt;
</description></item><item><title>re: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#9021353</link><pubDate>Wed, 29 Oct 2008 04:19:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9021353</guid><dc:creator>Eric Lippert</dc:creator><description>&lt;p&gt;Dave, I wrote this article over five years ago. Those problems have been fixed in that time.&lt;/p&gt;
&lt;p&gt;However, I still stand by my advice. Using closure semantics _unnecessarily_ adds complexity to the analysis of a program that many people do not intend or fully understand, and that's badness.&lt;/p&gt;
</description></item><item><title>Closures ??</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#9407318</link><pubDate>Sun, 08 Feb 2009 22:32:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9407318</guid><dc:creator>Journal of an Explorer</dc:creator><description>&lt;p&gt;A disclaimer first: This post will make sense to you only if you have read and understood my post on&lt;/p&gt;
</description></item><item><title>re: What are closures?</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#9463938</link><pubDate>Sat, 07 Mar 2009 08:32:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9463938</guid><dc:creator>configurator</dc:creator><description>&lt;p&gt;Why does the circular reference matter here? The browser never tears down the div, the reference to the function remains anyway, the GC cannot collect. In which point did the reference the function had to the div matter at all?&lt;/p&gt;
</description></item><item><title>  ??????Javascript?????????????????????????????? - Yaohaixiao&amp;#8217;g Blog</title><link>http://blogs.msdn.com/ericlippert/archive/2003/09/17/53028.aspx#9538044</link><pubDate>Wed, 08 Apr 2009 15:04:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9538044</guid><dc:creator>  ??????Javascript?????????????????????????????? - Yaohaixiao&amp;#8217;g Blog</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.yaohaixiao.com/?p=256"&gt;http://www.yaohaixiao.com/?p=256&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>