<?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>Overfloater &#187; groovy</title>
	<atom:link href="http://blog.thejit.org/category/groovy/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thejit.org</link>
	<description>Data Visualization, JavaScript and Computer Science related stuff</description>
	<lastBuildDate>Sun, 18 Jul 2010 13:38:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Taking a look at Groovy</title>
		<link>http://blog.thejit.org/2009/04/21/taking-a-look-at-groovy/</link>
		<comments>http://blog.thejit.org/2009/04/21/taking-a-look-at-groovy/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 17:11:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://blog.thejit.org/?p=639</guid>
		<description><![CDATA[While at work last week I decided to make a small program in the Groovy programming language. I needed to build a small file processing program that used some Java libraries built at work, but I didn&#8217;t want to code five or six Java classes to do so. Since performance wasn&#8217;t a big concern, I [...]]]></description>
			<content:encoded><![CDATA[<p>While at work last week I decided to make a small program in the <a href="http://groovy.codehaus.org/">Groovy</a> programming language. I needed to build a small file processing program that used some Java libraries built at work, but I didn&#8217;t want to code five or six Java classes to do so. Since performance wasn&#8217;t a big concern, I decided to take a look at some JVM based languages.</p>
<p>There are lots of programming languages targetting the JVM, so why Groovy?</p>
<h4>Why Groovy?</h4>
<p>Groovy is a highly dynamic language, that takes things from Python, Ruby and Smalltalk. Since these are programming languages I used before and I&#8217;m quite comfortable with, Groovy seemed like a good match.</p>
<p>Also, Groovy is very easy to learn, having an almost-zero learning curve. Since I had to do this in a couple of days, I didn&#8217;t want to spend a lot of time learning a programming language&#8217;s syntax and semantics. I know how to use Python and I know how to use Ruby/Smalltalk, I just want to do the same things in the JVM.</p>
<p>Another very interesting thing (that doesn&#8217;t concern the language itself, but it&#8217;s quite helpful) is that Groovy, along with OCaml and Perl, has a 100% completness score at <a href="http://pleac.sourceforge.net/">PLEAC</a>. That means that you can find complete examples of: Strings, Numbers, Arrays, Hashes, Dates and Times, Pattern Matching, File Access, File Contents, Directories, Subroutines, References and Records, Packages, Libraries and Modules, Classes and Objects, Database Access, User Interfaces and a lot more <a href="http://pleac.sourceforge.net/pleac_groovy/index.html">here</a>.</p>
<h4> Features I like about the language</h4>
<p>Some of the features I used and liked about Groovy:</p>
<p><b>List and Hash literals</b><br />
As opposed to Java, Groovy provides List and Hash literals:</p>
<pre name="code" class="java:nogutter:nocontrols">
//Create an empty List
emptyList = []
//Create an empty Hash
emptyHash = [:]
//Create a List
somePeople = ["John", "Jack", "Sarah"]
//Create a Hash
ext = [
  Ruby: 'rb',
  Python: 'py',
  C: 'c',
  Groovy: 'groovy'
]
</pre>
<p><b>List and Hash traversing and manipulation</b></p>
<p>In terms of List and Hash manipulation, Groovy offers the same expressiveness as Python and Smalltalk:</p>
<pre name="code" class="java:nogutter:nocontrols">
//Create a List
somePeople = ["John", "Jack", "Sarah"]

//Copy first two List elements
copy = somePeople[0..1] //["John", "Jack"]

//Grab last list element
lastElem = somePeople[-1] //"Sarah"

//Closures are first class values in Groovy, their syntax is {}.
//For unary lambdas an implicit 'it' variable is created

//Any element starting with capital letters?
somePeople.any { it[0] in 'A'..'Z' } //true

//We can also use regex a la Perl
somePeople.any { it =~ /[A-Z].*/ } //true

//Print names with new lines
somePeople.each { println it }

//Create a Hash
ext = [
  Ruby: 'rb',
  Python: 'py',
  C: 'c',
  Groovy: 'groovy'
]

//print an element
println ext.'Ruby' //rb
println ext['Ruby']//rb

//iterate through a hash elements
ext.each { key, value -> println key + ': ' + value }
//will print Ruby: rb, etc
</pre>
<p><b>Defining functions</b><br />
Functions are defined like this:</p>
<pre name="code" class="java:nogutter:nocontrols">
//Define a square function
def square(val) {
 val * val
}

//Or assign a closure to a square variable:
square = { it * it }
</pre>
<p>Simple.</p>
<p><b>Java classes extensions</b><br />
One of the things I find really nice about Groovy, is that it extends Java SE with useful functions.<br />
You can find the extensions <a href="http://groovy.codehaus.org/groovy-jdk/">here</a>.</p>
<p>Java File class extensions are pretty cool:</p>
<pre name="code" class="java:nogutter:nocontrols">
//Print dir file names
new File("/some/dir/").eachFile { println it.name }

//Print file text content
println new File("/some/file.txt").getText()

//Print file content with line numbers
new File("otherfile.txt").eachLine { it, line -&gt; println line + ": " + it }
</pre>
<p><b>Other libraries</b></p>
<p>At work I had to deal with XML data, and found a very interesting and high level XML manipulation library called <a href="http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html">XmlSlurper</a>:</p>
<pre name="code" class="java:nogutter:nocontrols">
xml = &#8216;&#8216;&#8216;
&lt;root&gt;
&lt;artist name="Pearl Jam"&gt;
  &lt;album&gt;Ten&lt;/album&gt;
  &lt;album&gt;Vs.&lt;/album&gt;
  &lt;album&gt;Vitalogy&lt;/album&gt;
  &lt;album&gt;Riot Act&lt;/album&gt;
&lt;/artist&gt;
&lt;artist name="Soundgarden"&gt;
  &lt;album&gt;Down on the Upside&lt;/album&gt;
  &lt;album&gt;Superunknown&lt;/album&gt;
&lt;/artist&gt;
&lt;/root&gt;
&#8216;&#8216;&#8216;
root = new XmlSlurper().parseText(xml)
root.artist.each {
  println it.@name.text() + ': ' +
        it.album.collect({ it.text() }).join(', ')
}
//Will print
//Pearl Jam: Ten, Vs., Vitalogy, Riot Act
//Soundgarden: Down on the Upside, Superunknown
</pre>
<h4>Conclusion</h4>
<p>Groovy is a versatile scripting language built on top of the JVM.<br />
It provides useful features taken from Ruby, Python and Smalltalk, with full access to all Java libraries.<br />
It also extends Java classes with useful methods and iterators.<br />
If you aren&#8217;t that worried about <a href="http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&#038;lang=all&#038;box=1">performance</a> (still runs faster than Ruby, Python 3 and Perl), I&#8217;d recommend you to take a look at <a href="http://pleac.sourceforge.net/pleac_groovy/index.html">it</a>.<br />
Hope this was helpful enough to get a feeling of the language.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.thejit.org/2009/04/21/taking-a-look-at-groovy/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
