Home > Clojure, Groovy > How Clojure improved my Groovy

How Clojure improved my Groovy

Dave Thomas and Andy Hunt have been saying for years that you should learn new programming languages. Doing so improves your skill in any language, whether you use the new language or not. I believed this with some skepticism, but decided nonetheless to start learning Clojure after reading The Pragmatic Programmer.

I’m reading and working through Programming Clojure, and one of the examples in the book looked like this:

(use '[clojure.contrib.str-utils :only (str-join)])
(str-join "-" ["hello", "clojure"])

Then, a week or so later, as I was working on some Groovy code, I saw something like the following:

def elements = ["It", "works", "on", "my", "machine!"]

def sb = new StringBuilder()
elements.eachWithIndex { element, index ->
  if (index == 0) {
  } else {
    sb.append(" ")

assert sb.toString() == "It works on my machine!"

And I thought, “man, this code would be so much better in Clojure!” Since I can’t use Clojure for my job (yet), I set out to find a better way to do this in Groovy. I decided to look for something similar to Clojure’s str-join for Groovy, and I found the join() method. It does exactly what I needed!

def elements = ["It", "works", "on", "my", "machine!"]

def sb = new StringBuilder()
sb.append(elements.join(" "))

assert sb.toString() == "It works on my machine!"

And so, I discovered for myself that Dave and Andy were right. Knowing Clojure (specifically, the clojure-contrib library) helped me to write better Groovy code.

  1. Dierk
    January 15, 2010 at 2:05 pm

    println elements.join(” “)
    all plain groovy, no contrib and no clojure inspiration required here

  2. January 17, 2010 at 4:30 am

    Yep no need to use a StringBuilder here either!

  3. January 18, 2010 at 11:40 am

    It’s very interesting article. Thank you for information.

  4. Dave
    January 19, 2010 at 10:20 am

    This doesn’t have anything to do with Clojure–it had to do with not knowing Groovy and/or libraries–almost every language has a “join” function either internal or provided by a third party. And even if it didn’t–wouldn’t this be amongst the first things someone would add to a string class or library?

  5. January 19, 2010 at 1:00 pm

    Well…not that strong of an observation. You could have had this same improvement doing many other mainstream languages too. Javascript, Ruby, PHP, or Python all have the String.join() method. I’d sum up your observation as Clojure’s API had a feature that you found handy, and you found a comparable feature in Groovy that you would have otherwise not used.

    I don’t think that’s why Andy and Dave advocate that though, or the benefit you speak of is very small compared to the benefit Andy and Dave are talking about. It’s quite a different thing to solve problems differently using Clojure’s language features. The key difference being an API call could easily be ported between languages where language concepts might not be. For example, monads in Haskell aren’t available in other languages. Solve a problem using Monads then think how you would solve that problem in another language that doesn’t support that language concept.

  6. January 19, 2010 at 8:37 pm

    @Charlie Thanks for your thoughts – I’ll have to learn about monads now.

  7. January 21, 2010 at 8:40 pm

    @Dave @Charlie I suppose I should have titled this post “How ‘Programming Clojure’ improved my Groovy,” because that’s really what happened here. I realize this is a result of not knowing the Groovy language, and I know that I could’ve discovered this by learning any language (Python, JS, Ruby). I think you’re both saying what I am – if you learn a new language and its features, you may discover other features in your language. My point in this post is that I found the join while reading Programming Clojure. And this is one reason to learn a new language, whether the benefit is small or large.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: