<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Joe Walker - closures tag</title>
  <link>http://directwebremoting.org/blog/joe/tags/closures/</link>
  <description>Thoughts on Web Development</description>
  <language>en</language>
  <copyright>Joe Walker</copyright>
  <lastBuildDate>Wed, 23 Jul 2008 11:00:41 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  
  <item>
    <title> Comparing the Evolution of Java and JavaScript</title>
    <link>http://directwebremoting.org/blog/joe/2007/12/17/comparing_the_evolution_of_java_and_javascript.html</link>
    
      
        <description>
          &lt;h2&gt;Java:&lt;/h2&gt;

&lt;p&gt;Java is the &lt;a href=&#034;http://www.tiobe.com/index.htm?tiobe_index&#034;&gt;most used language&lt;/a&gt; in the known universe. It is also a very simple* language. &lt;a href=&#034;http://gafter.blogspot.com/&#034;&gt;Some people&lt;/a&gt; believe that there is some scope in enabling Java to accommodate more programming styles (e.g. functional programming via &lt;a href=&#034;http://www.javac.info/&#034;&gt;closures&lt;/a&gt;). &lt;a href=&#034;http://www.javalobby.org/java/forums/t104586.html&#034;&gt;Others believe&lt;/a&gt; that being lightweight is a virtue.&lt;/p&gt;

&lt;h2&gt;JavaScript:&lt;/h2&gt;

&lt;p&gt;JavaScript is the &lt;a href=&#034;http://alex.dojotoolkit.org/?p=540&#034;&gt;most deployed language&lt;/a&gt; in the known universe. It is also a very simple* language. &lt;a href=&#034;http://weblogs.mozillazine.org/roadmap/archives/2007/11/my_media_ajax_keynote.html&#034;&gt;Some people&lt;/a&gt; believe that there is some scope in enabling JavaScript to accommodate more programming styles (e.g. &lt;a href=&#034;http://www.ecmascript.org/&#034;&gt;by adding classical inheritance not just prototypal inheritance, and an optional type system&lt;/a&gt;). &lt;a href=&#034;http://blogs.msdn.com/cwilso/archive/2007/10/31/what-i-think-about-es4.aspx&#034;&gt;Others believe&lt;/a&gt; that being lightweight is a virtue.&lt;/p&gt;

&lt;h2&gt;Features:&lt;/h2&gt;

&lt;p&gt;It&#039;s interesting that the current simple version of JavaScript already has the features (i.e. closures) that people want to keep out of Java otherwise it will become too complex, and that Java already has some of the features (i.e. classical inheritance) that people want to keep out of JavaScript, otherwise &lt;em&gt;it&lt;/em&gt; will become to complex.&lt;/p&gt;

&lt;h2&gt;Crossroads:&lt;/h2&gt;

&lt;p&gt;The Java language is challenged by languages such as Ruby, Groovy and even Scala, all of which support closures. The majority of people that have spent serious time with these languages prefer them over Java for at least some set of problems.&lt;/p&gt;

&lt;p&gt;The JavaScript language is challenged by Silverlight. Microsoft try to avoid saying things like &#034;death to Ajax&#034;, but secretly (or not so secretly depending on who you talk to) they would love to control the platform once more, and Silverlight is the latest attempt at regaining control.&lt;/p&gt;

&lt;h2&gt;Features vs. Complexity:&lt;/h2&gt;

&lt;p&gt;Joel noticed that there is a &lt;a href=&#034;http://www.joelonsoftware.com/articles/HighNotes.html&#034;&gt;big difference between good developers and bad developers&lt;/a&gt;. Google noticed it with it&#039;s picky hiring process. On many projects there are a few developers that do all the real work, while others just slow things down.&lt;/p&gt;

&lt;p&gt;But much of the &#034;keep complexity out of the language&#034; debate is aimed at helping the people that write less code anyway.&lt;/p&gt;

&lt;p&gt;Nail-guns and angle grinders are dangerous tools, but the building trade accepts the danger in the name of greater productivity. In software development where the difference between good and bad developers is that much greater, and where developers don&#039;t actually get physically maimed, the argument for power over simplicity is that much stronger.&lt;/p&gt;

&lt;p&gt;In summary, extra power helps smarter developers work more quickly, but makes life harder for less smart developers. Since a small number of smarter developers generally do better work than a large number of no so smart developers, we should tend slightly toward giving developers more powerful tools.&lt;/p&gt;

&lt;h2&gt;Java:&lt;/h2&gt;

&lt;p&gt;In Java, BGGA closures enable higher-order programming. It&#039;s hard to say that the addition of closures in and of itself would cause a complexity implosion, after all, Java is virtually the only modern day programming language without them. It could be argued that the Java language can&#039;t be evolved to accommodate them, but I&#039;m leaning towards there being a net gain by including them.&lt;/p&gt;

&lt;p&gt;Interestingly, &lt;a href=&#034;http://code.google.com/android/&#034;&gt;Google Android&lt;/a&gt; contains a clean room re-implementation of Java. If Sun (or the JCP) accepts closures, and if (as seems likely) Google votes against them, what will happen to Android? Will it follow how Google votes, or how Java evolves?&lt;/p&gt;

&lt;h2&gt;JavaScript:&lt;/h2&gt;

&lt;p&gt;With JavaScript, people see a neat, compact language and fear it turning into Java. The good news is that it won&#039;t fully turn into Java because JavaScript already has closures and no one thinks they should be taken out ;-) The danger of the &#034;features over complexity&#034; argument, is that it can be applied indiscriminately, creating a monster, and the fear is that JavaScript will become a monster.&lt;/p&gt;

&lt;p&gt;I&#039;m gradually coming round to the opinion that many of the changes proposed to JavaScript actually &lt;em&gt;reduce&lt;/em&gt; complexity. There are nearly as many frameworks that add classical inheritance to JavaScript as there are Ajax frameworks. Having one solution that we can all agree on can&#039;t be a bad thing even if you prefer prototypal inheritance.&lt;/p&gt;

&lt;hr/&gt;

&lt;p&gt;*: For some value of simple. The exact definitions of simple used in these 2 paragraphs are not necessarily the same.&lt;/p&gt;

&lt;script type=&#034;text/javascript&#034;&gt;
var dzone_url = &#039;http://getahead.org/blog/joe/2007/12/17/comparing_the_evolution_of_java_and_javascript.html&#039;;
var dzone_title = &#039;Comparing the Evolution of Java and JavaScript&#039;;
var dzone_blurb = &#039;Java is the most used language in the known universe, JavaScript the most deployed and there are some similarities in how they are growing&#039;;
var dzone_style = &#039;1&#039;;
&lt;/script&gt;

&lt;script language=&#034;javascript&#034; src=&#034;http://widgets.dzone.com/widgets/zoneit.js&#034;&gt;&lt;/script&gt;

        </description>
      
      
    
    
    
    <comments>http://directwebremoting.org/blog/joe/2007/12/17/comparing_the_evolution_of_java_and_javascript.html#comments</comments>
    <guid isPermaLink="true">http://directwebremoting.org/blog/joe/2007/12/17/comparing_the_evolution_of_java_and_javascript.html</guid>
    <pubDate>Mon, 17 Dec 2007 13:19:28 GMT</pubDate>
  </item>
  
  <item>
    <title>Emerging Java Technologies at QCon</title>
    <link>http://directwebremoting.org/blog/joe/2007/03/15/emerging_java_technologies_at_qcon.html</link>
    
      
        <description>
          &lt;p&gt;I&#039;ve just finished opening the &#034;&lt;a href=&#034;http://qcon.infoq.com/qcon/schedule/thursday.jsp&#034;&gt;Java Emerging Technologies&lt;/a&gt;&#034; track at &lt;a href=&#034;http://qcon.infoq.com/qcon/conference/&#034;&gt;QCon&lt;/a&gt; and spent some time talking about Closures. This is a summary of what I said:&lt;/p&gt;

&lt;p&gt;You can look at closures as blocks of code that you can pass around for execution. You can look at them as syntax sugar for inner classes. I think there is some significant power behind full closures that you miss when just looking at them from those perspectives.&lt;/p&gt;

&lt;p&gt;This is how you define an integer &#039;a&#039;, in Java:&lt;/p&gt;

&lt;pre&gt;int a;&lt;/pre&gt;

&lt;p&gt;This is how you define a function &#039;b&#039;, in a Java Interface:&lt;/p&gt;

&lt;pre&gt;String b(int p1, int p2);&lt;/pre&gt;

&lt;p&gt;This is how you define a closure &#039;c&#039;, according to the &lt;a href=&#034;http://www.javac.info/closures-v05.html
&#034;&gt;BGGA proposal&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;{ int, int =&amp;gt; String } c;&lt;/pre&gt;

&lt;p&gt;This defines a closure to which you pass 2 integers and it returns a String.&lt;/p&gt;

&lt;p&gt;Another closure &#039;d&#039; takes a String and returns nothing:&lt;/p&gt;

&lt;pre&gt;{ String =&amp;gt; } d;&lt;/pre&gt;

&lt;p&gt;We can provide an implementation of this closure:&lt;/p&gt;

&lt;pre&gt;
{ String =&amp;gt; } d = { String m =&gt;
    System.out.println(m);
};
&lt;/pre&gt;

&lt;p&gt;And then execute the closure like this:&lt;/p&gt;

&lt;pre&gt;d.invoke(&#034;Hello, World!&#034;);&lt;/pre&gt;

&lt;p&gt;The standard example (standard because it is the one used in the spec) looks like this:&lt;/p&gt;

&lt;pre&gt;
public static &amp;lt;T,throws E extends Exception&amp;gt;
T withLock(Lock lock, {=&amp;gt;T throws E} block) throws E {
    lock.lock();
    try {
        return block.invoke();
    } finally {
        lock.unlock();
    }
}
&lt;/pre&gt;

&lt;p&gt;This example can be made much easier to understand if we ignore exceptions and return types for a while:&lt;/p&gt;

&lt;pre&gt;
public static void withLock(Lock lock, {=&amp;gt;} block) {
    lock.lock();
    try {
        block.invoke();
    } finally {
        lock.unlock();
    }
}
&lt;/pre&gt;

&lt;p&gt;This is a method that takes a lock object and a closure. It then surrounds the execution of the closure with lock and unlock commands.&lt;/p&gt;

&lt;p&gt;Normally speaking you would use this as follows:&lt;/p&gt;

&lt;pre&gt;
withLock(lock, {=&amp;gt; System.out.println(&#034;hello&#034;); });
&lt;/pre&gt;

&lt;p&gt;However there is a special rule in the closures spec that says: If the last parameter to a method is a closure then you can write the closure outside the parameter list like this:&lt;/p&gt;

&lt;pre&gt;
withLock(lock) {
    System.out.println(&#034;hello&#034;);
}
&lt;/pre&gt;

&lt;p&gt;It&#039;s likely that we would not have needed the synchronized keyword if closures had been around in the beginning. It&#039;s also likely that a large amount of Java could have been defined in terms of closures. For example:&lt;/p&gt;

&lt;pre&gt;
public static &amp;lt;throws E extends Exception&amp;gt;
void If(boolean condition, {=&amp;gt; throws E} block) throws E {
    if (condition) {
        block.invoke();
    }
}
&lt;/pre&gt;

&lt;p&gt;You would use this function like this (note the capital I on If:&lt;/p&gt;

&lt;pre&gt;
If (a == b) {
  System.out.println(&#034;a equals b&#034;);
}
&lt;/pre&gt;

&lt;p&gt;So closures as defined by the BGGA proposal allow you to do some fairly fundamental things with the Java language.&lt;/p&gt;

        </description>
      
      
    
    
    
    <comments>http://directwebremoting.org/blog/joe/2007/03/15/emerging_java_technologies_at_qcon.html#comments</comments>
    <guid isPermaLink="true">http://directwebremoting.org/blog/joe/2007/03/15/emerging_java_technologies_at_qcon.html</guid>
    <pubDate>Thu, 15 Mar 2007 13:42:19 GMT</pubDate>
  </item>
  
  <item>
    <title>Closures: Cheers, Library Issues and Solutions</title>
    <link>http://directwebremoting.org/blog/joe/2006/08/19/closures_cheers_library_issues_and_solutions.html</link>
    
      
        <description>
          &lt;p&gt;&lt;a href=&#034;http://gafter.blogspot.com/2006/08/closures-for-java.html&#034;&gt;At last&lt;/a&gt;. But there is a problem...&lt;/p&gt;

&lt;p&gt;I think the addition of Closures are the biggest change yet made to Java. Closures change the way you think about programs - a step change rather than a simple evolution like Generics were.&lt;/p&gt;

&lt;p&gt;It&#039;s good to be able to do this:&lt;/p&gt;

&lt;pre&gt;
int total = 0;
void(int) totalizer = (int i) : total = total + i;
totalizer(1);
totalizer(2);
System.out.println(total); // Prints 3
&lt;/pre&gt;

&lt;p&gt;But what you really want is to be able to do things like this:&lt;/p&gt;

&lt;pre&gt;
int[] numbers = new int[] { 100, 200 };
numbers.each(totalizer);
System.out.println(total); // Now prints 303;
&lt;/pre&gt;

&lt;p&gt;This is a fairly serious problem. Without support from many of the system classes - especially Collections, closures are really quite limited. So this is why it&#039;s a real shame that it has taken so long to get Closures into Java.&lt;/p&gt;

&lt;p&gt;I think the most useful additional collection methods would be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;each - run the closure once for each collection element&lt;/li&gt;
&lt;li&gt;transform - return a collection of elements that are returned by using the closure once for each input collection element&lt;/li&gt;
&lt;li&gt;all - return true if the closure returns true for all collection elements&lt;/li&gt;
&lt;li&gt;any - return true if the closure returns true for any collection elements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the face of it there are 3 options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;break backward compatibility 
&lt;li&gt;break closures by lack of collection support&lt;/li&gt;
&lt;li&gt;add static methods like &lt;code&gt;Collections.each(Collection c, Closure x);&lt;/code&gt;
&lt;/ol&gt;

&lt;p&gt;Option 1 may seem like a good idea to many, but it won&#039;t happen. Sun are very strong on keeping backwards compatibility.&lt;/p&gt;

&lt;p&gt;Option 2 is just giving in without trying.&lt;/p&gt;

&lt;p&gt;Option 3 maybe the best, but it&#039;s not really a great answer.&lt;/p&gt;

&lt;p&gt;However I wonder if there isn&#039;t a fourth option: Some extra syntax sugar like this could replace the need for things like &lt;code&gt;Collection.each(Closure c)&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;
for (numbers : totalizer);
&lt;/pre&gt;

&lt;p&gt;Behind the scenes the compiler does this:&lt;/p&gt;

&lt;pre&gt;
for (Object obj : numbers.iterator) {
  totalizer(obj);
}
&lt;/pre&gt;

&lt;p&gt;Clearly this should work with anything that is Iterable and not just lists.&lt;/p&gt;

&lt;p&gt;However, this doesn&#039;t fix the lack of &lt;code&gt;all()&lt;/code&gt;  and &lt;code&gt;any()&lt;/code&gt; methods or &lt;code&gt;transform()&lt;/code&gt; (unless we allow a return value from the new for loop). Might it be possible to create a syntax like this to cope with all eventualities? Maybe the new for syntax could optionally take 2 closures, the first was an action to take on each collection element and the second was a combiner that allowed you to specify how the loop functioned???&lt;/p&gt;

        </description>
      
      
    
    
    
    <comments>http://directwebremoting.org/blog/joe/2006/08/19/closures_cheers_library_issues_and_solutions.html#comments</comments>
    <guid isPermaLink="true">http://directwebremoting.org/blog/joe/2006/08/19/closures_cheers_library_issues_and_solutions.html</guid>
    <pubDate>Sat, 19 Aug 2006 14:35:04 GMT</pubDate>
  </item>
  
  <item>
    <title>7 Things I Learnt at TSS-Europe</title>
    <link>http://directwebremoting.org/blog/joe/2006/06/26/7_things_i_learnt_at_tss_europe.html</link>
    
      
        <description>
          &lt;p&gt;1. I learnt that &lt;a href=&#034;http://retroweaver.sourceforge.net/&#034;&gt;Retroweaver&lt;/a&gt;  actually works. Retroweaver tweaks your JDK5 compiled classes to make them work on JDK1.4. The JavaPosse had some doubts about it a while ago, but &lt;a href=&#034;http://radio.weblogs.com/0112098/&#034;&gt;James Strachan&lt;/a&gt; and &lt;a href=&#034;http://rifers.org/blogs/gbevin&#034;&gt;Geert Bevin&lt;/a&gt; love it. I&#039;m thinking that DWR v2 will probably adopt this and drop the support for JDK 1.3. Please shout now if you need us to keep JDK 1.3 support.&lt;/p&gt;

&lt;p&gt;2. I learnt that red wine and light blue t-shirts don&#039;t mix. Sorry Nathalie.&lt;/p&gt;

&lt;p&gt;3. I learnt that &lt;a href=&#034;http://en.wikipedia.org/wiki/Ocaml&#034;&gt;OCAML&lt;/a&gt; is interesting language to teach your brain new tricks. Bruce Tate was talking Ruby up, as expected, and saying that programmers should learn a new language on a regular basis to keep on their toes. However I spent the flight home chatting to &lt;a href=&#034;http://www.cs.bham.ac.uk/~aps/&#034;&gt;Alan Sexton&lt;/a&gt; who convinced me to take a look at OCAML and a better mind stretching exercise.&lt;/p&gt;

&lt;p&gt;4. I learnt that a simple &#034;hola&#034; (hello) makes a big difference in Spain. I&#039;m not great at foreign languages - I can get by in French, but only badly. I got an extra legroom seat on the flight home for nothing. It&#039;s either because the girl at the check-in was magnetically attracted to my rugged good looks, or because I smiled and said Hola. Anyone that has actually seen my rugged good looks will agree that Hola makes a big difference.&lt;/p&gt;

&lt;p&gt;5. I learnt that continuations and closures still confuse lots of people. They are very different. Continuations are when you freeze a thread and serialize the stack to be re-run later. Closures are functions that can be assigned to variables where the functions can refer to variables in the enclosing scope. Nothing to do with each other, but often confused.&lt;/p&gt;

&lt;p&gt;6. I learnt that it is a real shame industry and academia doesn&#039;t mix more. I can&#039;t see much in the Ajax world coming out of academia just because the Ajax world is spinning so fast, but there are some brains there that could certainly teach us a thing or two.&lt;/p&gt;

&lt;p&gt;7. I learnt that many top universities still don&#039;t teach copyright/patent law as a mandatory part of their courses. This is a huge hole. How can you be a good programmer without knowing the basics of what ideas you can copy from others and when?&lt;/p&gt;

        </description>
      
      
    
    
    
    <comments>http://directwebremoting.org/blog/joe/2006/06/26/7_things_i_learnt_at_tss_europe.html#comments</comments>
    <guid isPermaLink="true">http://directwebremoting.org/blog/joe/2006/06/26/7_things_i_learnt_at_tss_europe.html</guid>
    <pubDate>Mon, 26 Jun 2006 10:53:07 GMT</pubDate>
  </item>
  
  </channel>
</rss>
