Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

what is a cool use of groovy

 
author & internet detective
Posts: 40912
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 40912
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
gunslinger & author
Posts: 158
11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Posts: 158
    11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Posts: 40912
    840
    Eclipse IDE VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Very cool. Thanks Ken!
     
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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..
     
    Bartender
    Posts: 2407
    36
    Scala Python Oracle Postgres Database Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Does Gradle count?
    reply
      Bookmark Topic Watch Topic
    • New Topic