As I have seen many questions asking tips for golfing in their interested language, I decided to ask for my favorite language: Groovy. People can give some tips and tricks that will be used in golfing with Groovy.

  • 1
    \$\begingroup\$ Well this isn't very popular, is it :-( \$\endgroup\$
    – Armand
    Nov 29, 2011 at 20:01
  • \$\begingroup\$ @Alison : I feel Bad, as there is no Golf tip for Groovy in this site :( \$\endgroup\$
    – Ant's
    Nov 30, 2011 at 5:25
  • 2
    \$\begingroup\$ Do you have any tips to start us off? \$\endgroup\$
    – Armand
    Nov 30, 2011 at 11:50

4 Answers 4


I'm new to this whole golfing thing, this is what I got so far:

Use Closures not functions:

def a(b){print b}

is longer than

a={print it}

You can use a negative index in arrays and lists as an alias for size()-

c = "abc"
d = ["a", "b", "c"]

assert c[c.size()-1] == c[-1]
assert c[c.size()-2] == c[-2]
assert d[d.size()-1] == d[-1]
assert d.last() == d[-1]

The spread operator is a shortcut for collect:

assert d*.size() == d.collect{it.size()}

For sorting use the spaceship operator:

e = [54,5,12]
assert e.sort{a,b->a<=>b}==e.sort{a,b->a<b?-1:+1}
assert e.sort{a,b->a<=>b}==e.sort{a,b->if (a>b) {return(-1)} else {return(+1)}}

Edit Conversions:

assert "123" as int == "123".toInteger()
  • \$\begingroup\$ if on something listish the *.X spread operator can often be written as .X \$\endgroup\$
    – cfrick
    Jul 7, 2014 at 20:50

As Groovy is a somewhat verbose language, you could use Groovys MOP to shorten method calls.

The following snippet for example would pay off after the fourth usage:


Tip golfing edit:


Or, you know:


grep is short and works on several problems

get the chars of a string as a list with spaces and without leading elements: 'ABC XYZ'.grep() returns [A, B, C, , X, Y, Z]

grep with regexp is shorther than converting to upper case, if required: it.grep(~/(?i)$c/) instead of it.toUpperCase().grep(c)

  • 1
    \$\begingroup\$ 'ABC XYZ'as Set is one character shorter and works in most of the same places \$\endgroup\$
    – Marty Neal
    Jun 14, 2018 at 15:01

Getting An Array from a List of Objects

If you have a list of objects like:

def users = [[user:'A',id:1],[user:'B',id:2],[user:'C',id:3]]

You can generate an ArrayList with a certain property using:

def userIds = users*.id // [1, 2, 3] no explicit loops!

BONUS: Groovy... on Rails!

Well, in Grails Framework we must get many values from a select with multiple attribute. You can use loops, flatten and other programming more typical structures, but you can save some lines. If you have a select like:

<select id="users" multiple="true" name="users">
    <option value="193">User A</option>
    <option value="378">User B</option>
    <option value="377">User C</option>
</select><%-- No Grails tags for now --%>

Then, in your controller you can simple write:

def aListOfUsers = User.getAll(params.list('userIds'))
  • 1
    \$\begingroup\$ Would this question accept Grails golfing tips? I confess I'm afraid of downvote snipers... \$\endgroup\$
    – Victor F
    Aug 28, 2015 at 18:49
  • 2
    \$\begingroup\$ Don't be afraid, PCG is full of extremely nice people :) \$\endgroup\$
    – Beta Decay
    Aug 28, 2015 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.