I felt that it was an unforgivable omission to not have Groovy included in the plethora of covered languages in these posts and embarked on writing my own examples. It was only after I finished the first eight that I realized that Arturo Herrero had already addressed this glaring omission in the post 10 Groovy One Liners to Impress Your Friends. Because I had already done most of the work on my own post, I have decided to include my example and this post anyway. It is interesting to see how many things I did the same way, but there are some minor differences in some of the one-liners. I did not re-invent #9, #10, and the bonus #11 after I realized the existence of this other post. I have included the same approaches for those last three in my example here for completeness. Note that there is also a different take on the Scala example as well. If Scala gets two perspectives, shouldn't Groovy get two as well?

It was as a I was about to write an example of Groovy parallel processing using GPars that I discovered the existence of Arturo Herrero's blog post. I use a slightly adapted version of his example here. It assumes the existence of an initialized dataList and the existence of a method called processItem.

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print "FizzBuzz".

It is arguable that the Groovy version is even more readable than the CoffeeScript versions, though that opinion is undoubtedly affected by my greater familiarity with Groovy syntax.

The Value of the One Liner Exercise

Dion Almaer has stated in an appropriately geeky way that "the 'best' programming language, doesn’t mean the one that creates the lowest wc -l." However, in the same post, he also points out that "languages such as Ruby [and] Groovy ... allow me to get closer to the zen of 'expressing everything I want, and need to get across... but not more'. Every operator/method tells me a lot."

I believe that the examples in this post and in the other "One Liners" posts provide examples and the good and the bad of one liners. Some of the examples here (such as getting the minimum and maximum from a collection, getting a file's contents, and summing a collection's elements) are concise and highly readable at the same time. Most large scale software project cannot afford the unmaintainable and unreadable code that can come from cramming a bunch of functionality into a single line via archaic representations. However, a language with a carefully crafted syntax can, as some of these examples show, allow for concise and highly readable expression. It's really advantageous when one can get readability and conciseness.

I found personal benefit from writing these examples and writing this post. For one, I was happy to realize that I'm definitely far more comfortable with Groovy and Groovy-isms than I was when I first started using Groovy. A second benefit of this exercise was exposure to new Groovy concepts. Before writing these examples, I had never used the Collection.collect(Closure) or Collection.inject(Object, Closure) methods. In trying to figure out how to implement these one liners, I also was reminded how nice the GDK API documentation is because I was able to figure out Groovy equivalents using that documentation. It was a nice challenge to work out some Groovy solutions in as concise a manner as possible and that challenge made this exercise even more useful.

Although Arturo Herrero's blog post and my blog post provide Groovy implementations of these 10+1 one liners, it is likely that Groovy developers out there can make some of these examples even Groovier. If you can, please pass along those improvements via comment on this blog, via your own blog, or via Tweet. Many of the one liners posts for the other languages had community feedback on ways to improve or at least provide an alternative to the original authors' ideas.

Other Groovy One Liners

I'm not going to do it here because this post is already sufficiently lengthy, but I think an interesting idea for a future post (or for someone else to post) is another "set" of one liner implementations written in Groovy. For example, I'm a big fan of this one liner in Groovy for printing out the available TimeZones for a particular JVM implementation.

Thanks to Marcus Kazmierczak for starting off this multi-language coverage of one liners. It has made for interesting reads of others' posts and how those languages are similar and different in resolving the same issues. It has also led to a very insightful activity in implementing Groovy equivalents of these one liners. This exercise has been a reminder of the concise expressiveness of the Groovy language (the "in" crowd calls it "fluency"). Groovy is certainly a fluent language.

9 comments:

When I was about to start work on a Groovy implementation of the Sieve of Eratosthenes, I was glad to see that you already had one out there. Thanks for the post and for taking the time to provide feedback here.