<?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...: Groovy Fizz and Buzz</title>
    <link>http://blog.fredjean.net/articles/2008/02/28/groovy-fizz-and-buzz</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>
    <item>
      <title>"Groovy Fizz and Buzz" by Tim Yates</title>
      <description>Sorry...me again...  For completeness (and full Groovy-ness), I did it using Categories  ;)
&lt;pre&gt;
class FizzCategory {
  static Object fizz( Integer i ) {
    ( i % 3 ? "" : "Fizz" ) &amp;lt;&amp;lt; ( i % 5 ? "" : "Buzz" ) ?: i
  }
}
use( FizzCategory ) {
  (1..100)*.fizz().each { println it }
}&lt;/pre&gt;</description>
      <pubDate>Fri, 29 Feb 2008 13:21:18 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b5cbd287-7ee2-45be-bb79-a10ced2aecbe</guid>
      <link>http://blog.fredjean.net/articles/2008/02/28/groovy-fizz-and-buzz#comment-21</link>
    </item>
    <item>
      <title>"Groovy Fizz and Buzz" by Jason S</title>
      <description>This is less groovy but pretty short:
(1..100).each{s=(it%3?"":"fizz")+(it%5?"":"buzz");println s==""?it:s}
</description>
      <pubDate>Thu, 28 Feb 2008 23:51:02 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b822391e-651f-4ae7-b2eb-9165bcff0161</guid>
      <link>http://blog.fredjean.net/articles/2008/02/28/groovy-fizz-and-buzz#comment-20</link>
    </item>
    <item>
      <title>"Groovy Fizz and Buzz" by Frederic Jean</title>
      <description>&lt;p&gt;Tim,&lt;/p&gt;
&lt;p&gt;
Now I'm having the d'oh! moment ;). I didn't think about extending the Integer class. Very nice implementation.&lt;/p&gt;
&lt;p&gt;Fred&lt;/p&gt;</description>
      <pubDate>Thu, 28 Feb 2008 14:54:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:92fe8ff1-d333-4d1e-b882-44375ea69c53</guid>
      <link>http://blog.fredjean.net/articles/2008/02/28/groovy-fizz-and-buzz#comment-19</link>
    </item>
    <item>
      <title>"Groovy Fizz and Buzz" by Tim Yates</title>
      <description>I'm an idiot... *blush*
&lt;pre&gt;
Integer.metaClass.fizz = { -&gt;
  ( delegate % 3 ? "" : "Fizz" ) &amp;lt;&amp;lt; ( delegate % 5 ? "" : "Buzz" ) ?: delegate
}
(1..100)*.fizz().each { println it }&lt;/pre&gt;
That's it...I promise...</description>
      <pubDate>Thu, 28 Feb 2008 14:13:33 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e55c75dc-9ed8-4b2c-b597-dc1508d4bcb2</guid>
      <link>http://blog.fredjean.net/articles/2008/02/28/groovy-fizz-and-buzz#comment-18</link>
    </item>
    <item>
      <title>"Groovy Fizz and Buzz" by Tim Yates</title>
      <description>Sorry...me again...  I got carried away...
&lt;pre&gt;
Integer.metaClass.fizzbuzz = { -&gt;
  def r
  delegate.each { 
    r = ( it % 3 ? "" : "Fizz" ) &amp;lt;&amp;lt; ( it % 5 ? "" : "Buzz" ) ?: it
  }
  r
}
(1..100)*.fizzbuzz().each { println it }&lt;/pre&gt;</description>
      <pubDate>Thu, 28 Feb 2008 13:35:34 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0a9d5260-a522-4ff8-ab9f-5b90554840ac</guid>
      <link>http://blog.fredjean.net/articles/2008/02/28/groovy-fizz-and-buzz#comment-17</link>
    </item>
    <item>
      <title>"Groovy Fizz and Buzz" by Tim Yates</title>
      <description>Or, as Groovy allows conditionals in a C style, the following works as well...
&lt;pre&gt;
(1..100).collect {
  ( it % 3 ? "" : "Fizz" ) &amp;lt;&amp;lt; ( it % 5 ? "" : "Buzz" ) ?: it
}.each { println it }&lt;/pre&gt;
Maybe even less readable, but less code also ;)</description>
      <pubDate>Thu, 28 Feb 2008 10:05:04 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e8ae23a3-85ac-4512-91ac-4403dfb00055</guid>
      <link>http://blog.fredjean.net/articles/2008/02/28/groovy-fizz-and-buzz#comment-16</link>
    </item>
  </channel>
</rss>
