<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>indiWiz.com &#187; closure</title>
	<atom:link href="http://indiwiz.com/tag/closure/feed/" rel="self" type="application/rss+xml" />
	<link>http://indiwiz.com</link>
	<description>Subhash&#039;s Tech Log</description>
	<lastBuildDate>Sun, 29 Apr 2012 15:11:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Beautiful Closures</title>
		<link>http://indiwiz.com/2008/11/18/beautiful-closures/</link>
		<comments>http://indiwiz.com/2008/11/18/beautiful-closures/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 08:27:54 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://indiwiz.wordpress.com/?p=6</guid>
		<description><![CDATA[One of the irritating things when writing code in Java is when managing files or database resources. We have to write so much redundant code: So much redundancy is ground for human errors. Issues like these are beautifully solved using programming concept called Closures. For example, in the newly released 2.6 version of Python, the [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>One of the irritating things when writing code in Java is when managing files or database resources. We have to write so much redundant code:</p>
<pre class="brush: java; title: ; notranslate">
File f = new File(&quot;/etc/passwd&quot;);
BufferedReader br = null;
try{
  br = new BufferedReader(new FileReader(f));
  String str;
  while((str=br.readLine())!=null){
    System.out.println(str);
  }
}
catch(IOException ex){
  ...
}
finally{
  if(br != null){
    try{
      br.close();
    }
    catch(IOException ex){
      ...
    }
  }
}
</pre>
<p>So much redundancy is ground for human errors. Issues like these are beautifully solved using programming concept called Closures. For example, in the newly released 2.6 version of Python, the same would be written as:</p>
<pre class="brush: python; title: ; notranslate">
with open('/etc/passwd', 'r') as f:
    for line in f:
        print line
</pre>
<p>In the above code, even when an exception is raised during file read, the file is gracefully closed when the control exits with block. A similar example in Groovy:</p>
<pre class="brush: java; title: ; notranslate">
File f = new File(&quot;/etc/passwd&quot;)
f.eachLine{
  line -&gt;
  println line
}
</pre>
<p>Martin Fowler has written a short introduction to <a href="http://martinfowler.com/bliki/Closure.html">Closures</a>.</p>
<div class="shr-publisher-6"></div><!-- Start Shareaholic LikeButtonSetBottom --><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2008/11/18/beautiful-closures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

