wood burning stoves 2.0*
The moose likes Groovy and the fly likes what is a cool use of groovy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "what is a cool use of groovy" Watch "what is a cool use of groovy" New topic
Author

what is a cool use of groovy

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31074
    
232

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Don't know how cool it is, but the Play! framework (version 1) templates use Groovy rather than JSP. (Version 2 uses Scala templates.)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31074
    
232

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

Joined: Sep 18, 2002
Posts: 89
    
    5
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


Kenneth A. Kousen, Ph.D. (assorted certs), President, Kousen IT, Inc.
Author of Making Java Groovy - http://www.kousenit.com
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

    Joined: Sep 18, 2002
    Posts: 89
        
        5
    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

    Joined: May 26, 2003
    Posts: 31074
        
    232

    Very cool. Thanks Ken!
    Pavel Sok
    Greenhorn

    Joined: Nov 23, 2013
    Posts: 10
    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

    Joined: Mar 01, 2009
    Posts: 1873
        
      16

    Does Gradle count?


    No more Blub for me, thank you, Vicar.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: what is a cool use of groovy