Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is a cool use of groovy

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm curious what cool uses people have found for Groovy. (beyond a Grails app)

MIne is being able to write Hudson/Jenkins scripts to run in the Groovy console. It's awesome being able to fix the parent jobs of hundreds of projects in just a few lines of code. Or write queries/reporting in just a few lines of code.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know how cool it is, but the Play! framework (version 1) templates use Groovy rather than JSP. (Version 2 uses Scala templates.)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Don't know how cool it is, but the Play! framework (version 1) templates use Groovy rather than JSP. (Version 2 uses Scala templates.)

So people can put too much Groovy code in the template instead of too much Java code?
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 100
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a one-liner to amuse your friends. Get the URL of their web site, open up a Groovy console, and do this:

The result prints the HTML code from the web site. That's one step away from accessing a RESTful web service. I have lots of examples like that.

Ken
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Bear Bibeault wrote:Don't know how cool it is, but the Play! framework (version 1) templates use Groovy rather than JSP. (Version 2 uses Scala templates.)

So people can put too much Groovy code in the template instead of too much Java code?

LOL! Yeah, those without discipline can create really poor templates. But in a lot of ways it's very very cool.

Two things I really like:
  • The "Elvis" operator: ?: (great for supplying defaults on false/null).
  • "Safe nav" operator: short-circuits null references. The expression user?.company?.address?.city produces null, rather than an NPE, if any reference is null.
  •  
    Kenneth A. Kousen
    gunslinger & author
    Ranch Hand
    Posts: 100
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Speaking of the Elvis operator, try this one:

    That's a sort by length, and if the lengths are equal (i.e., the sizes are the same so <=> returns 0), it sorts lexicographically (which is alphabetically with capital letters ahead of lowercase letters). More importantly, it shows Elvis being carried back to his home planet by two tandem spaceships, so it's the fat Elvis from the 70s rather than the thin Elvis from the 50s.

    Yeah, that's a long way to go for such a silly gag, but I still find it amusing. More generally, if you wanted to sort strings by length in Java, you'd write:

    The Groovy JDK adds a sort method to collections, so you don't need the Collections class at all. The sort method also takes a one- or two-argument closure. If you use the two-arg closure, you supply a typical comparator, as in:

    If you use the one-arg closure, then just return something that can be used for the sort (typically an integer), so the whole length sort reduces to:

    and you're done. Hard to be much simpler than that.
     
    Jeanne Boyarsky
    author & internet detective
    Marshal
    Posts: 34396
    346
    Eclipse IDE Java VI Editor
     
    Pavel Sok
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We use groovy as additional logic in jasperreports (so-called scriptlets) and in testing. That's all, unfortunately. Nobody wants to use it in production in our team..
     
    chris webster
    Bartender
    Posts: 2407
    32
    Linux Oracle Postgres Database Python Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Does Gradle count?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic