Groovy Bar & Grill

December 9, 2008

ReverseEcho.groovy

Filed under: Groovy Grill — jawild59 @ 10:23 pm

There’s a great line from the movie, The Truman Show, that explains why Truman never questioned his life circumstances: “We accept the reality of the world with which we’re presented.” It’s the same with Java. We didn’t know how tedious it was until Groovy showed us a better way.
Just how tedious is Java. Let’s look at an example that prints out the reverse of the command line arguments (e.g. “This is a test” prints out as “tset a si sihT”)


public class ReverseEcho {
  public static void main(String[] args) {
    for (int i = args.length - 1; i >= 0; i--) {
      for (int j = args[i].length() 1; j >= 0; j--) {
        System.out.print(args[i].charAt(j));
      }
      System.out.print(" ");
    }
  }
}
  

When you don’t know any better, well, that’s what you have to do: set up nested for loops and traverse in reverse. You’re happy as a bit in a bucket until Groovy comes along and presents a new reality.


args.reverseEach {
  print "${it.reverse()} "
}

Yep, Groovy really makes it that easy. args is an implicitly defined list that contains the command line arguments. reverseEach() is a shortcut (a concept mostly absent in Java) for reverse().each() and iterates over each element in the list in reverse order. reverse() is also a GDK method on string and does exactly what you might think, it creates a new string which is the reverse of the original.

Fibonacci.groovy

Filed under: Groovy Grill — jawild59 @ 6:49 pm

One of the ugliest constructs in Java is the for loop. The enhanced for in Java 5 improved readability but Groovy has alternatives that are even better. Let’s take a look. Here is Java code (from Java Examples in a Nutshell, 2nd Ed.) that prints the first 20 Fibonacci numbers:


public class Fibonacci {
  public static void main(String[] args) {
    int n0 = 1, n1 = 1, n2;
    System.out.print(n0 + " " + n1 + " ");

    for (int i = 0; i < 18; i++) {
      n2 = n1 + n0;
      System.out.print(n2 + " ");
      n0 = n1;
      n1 = n2;
    }
  }
}
  

Typical stuff; for is utilized to execute a loop a certain number of times, in this case eighteen. Groovy handles this much better.


def list = [01]

20.times {
  print "${list.last()} "
  list << list.sum()
  list = list.tail()
}

Because everything’s an object in Groovy, 20 is an Integer with all the extra methods supplied by the GDK. The times() method effectively executes the closure 20.intValue() number of times.

There’s some cool stuff going on with the Groovy list too; Instead of using 3 variables to track 2 values, we use a variable size list. Each iteration of the closure prints the last element in the list. Next, we sum up all the list elements and push the value onto the end of the list. list.trail() then lops off the first element in the list so we’re left with the next two numbers in the Fibonacci series.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.