<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Out of my mind...: Tag groovy</title>
    <link>http://blog.fredjean.net/articles/tag/groovy</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Frederic Jean's Random Thoughts</description>
    <item>
      <title>Groovy Fizz and Buzz</title>
      <description>&lt;p&gt;The &lt;a href="http://tickletux.wordpress.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/"&gt;FizzBuzz programming interview&lt;/a&gt; question &lt;a href="http://www.dougalstanton.net/blog/index.php/2008/02/26/my-shame-is-complete"&gt;raised it's head again&lt;/a&gt;. I've managed to resist the temptation to implement it in any languages so far. Until this morning that is...&lt;/p&gt;
&lt;p&gt;So I fired up the Groovy Console and played a little. A nice feature of the console is that it displays a history of the scripts it ran. This allows me to present an evolution of my FizzBuzz program.&lt;/p&gt;
&lt;p&gt;I first started with a rather naive implementation:&lt;/p&gt;
&lt;pre&gt;
(1..100).each { 
  def out = "" 
  if ((it % 3) == 0) { out += "Fizz" } 
  if ((it % 5) == 0) { out += "Buzz" } 
  if (out == "" ) { out = it } 
  println out
}
&lt;/pre&gt;
&lt;p&gt;Not bad for something that I came up with in about 10 minutes. It doesn't really take advantage of Groovy's features though. So I refined it a bit by using the ternary operator:&lt;/p&gt;
&lt;pre&gt;
(1..100).each {
  def out = (it % 3) == 0 ? "Fizz" : ""
  out += (it % 5) == 0 ? "Buzz" : ""
  println (out == "" ? it : out)
}
&lt;/pre&gt;
&lt;p&gt;This is a slight improvement over the first version. I still didn't feel that this was Groovy enough for me. I decided to play with the List collect method. This applies a closure to all items in a collection. The first pass was the following code segment:&lt;/p&gt;
&lt;pre&gt;
println ((1..100).collect {
  def out = (it % 3) == 0 ? "Fizz" : ""
  out += (it % 5) == 0 ? "Buzz" : ""
  out == "" ? it : out
})
&lt;/pre&gt;
&lt;p&gt;This ended up printing the list returned by the collect method. Really close, but not quite what I was hoping for. After all, the challenge is to print the results, not quite a string representation of a list. It was easy then to go from the code above to the code below:&lt;/p&gt;
&lt;pre&gt;
(1..100).collect {
  def out = (it % 3) == 0 ? "Fizz" : ""
  out += (it % 5) == 0 ? "Buzz" : ""
  out ?: it
}.each { println it }
&lt;/pre&gt;
&lt;p&gt;This printed the list just like the first pass. It's definitively Groovier (in my opinion) than the original implementation. It still does fall short of the capabilities of Groovy.&lt;/p&gt;
&lt;p&gt;So here's my final implementation:&lt;/p&gt;
&lt;pre&gt;
(1..100).collect {
     ((it % 3) == 0 ? "Fizz" : "") &amp;lt;&amp;lt; ((it % 5) == 0 ? "Buzz" : "") ?: it
}.each { println it }
&lt;/pre&gt;
&lt;p&gt;This takes full advantage of Groovy's power. It is a little harder to read, which is a downside. It does take full advantage of many of Groovy's powerful features:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;A range to generate the list of numbers to process. Ranges return an iterator that iterates from the start of the range to the end of the range.&lt;/li&gt;

  &lt;li&gt;The collect method which applies a closure to each item in the collection (or iterator) and returns the results as a list.&lt;/li&gt;

  &lt;li&gt;The each method, which iterates through a list and applies a closure to it. It returns the collection that it iterated through.&lt;/li&gt;

  &lt;li&gt;Closures, which are implemented quite well in Groovy.&lt;/li&gt;

  &lt;li&gt;Operator overloading. The &amp;lt;&amp;lt; operator is overloaded on the String class to concatenate two Strings together.&lt;/li&gt;

  &lt;li&gt;The brand new Elvis operator (?:) which returns the alternate value if the expression evaluates to false.&lt;/li&gt;

  &lt;li&gt;Groovy returns the last expression of a block as the result of the block. This includes closures and methods.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I do think that I'll incorporate this question in future interviews. It has quite a few interesting subtleties and implementations that are only rivaled by the Singleton pattern. I guess that the last group of interviewees got it a little easier...&lt;/p&gt;&lt;br /&gt;


</description>
      <pubDate>Thu, 28 Feb 2008 06:57:02 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f4b7533a-6d5e-4188-b95e-6747b2903c15</guid>
      <author>fred@fredjean.net (Frederic Jean)</author>
      <link>http://blog.fredjean.net/articles/2008/02/28/groovy-fizz-and-buzz</link>
      <category>Groovy</category>
      <category>General</category>
      <category>groovy</category>
      <category>programming</category>
      <category>fizzbuzz</category>
    </item>
  </channel>
</rss>
