Sign up ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
Well this isn't very popular, is it :-( – Alison Nov 29 '11 at 20:01
@Alison : I feel Bad, as there is no Golf tip for Groovy in this site :( – Ant's Nov 30 '11 at 5:25
Do you have any tips to start us off? – Alison Nov 30 '11 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()
share|improve this answer
if on something listish the *.X spread operator can often be written as .X – cfrick Jul 7 '14 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:

share|improve this answer
Very cool. (filler) – J Atkin Nov 21 at 21:40

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)

share|improve this answer

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'))
share|improve this answer
Would this question accept Grails golfing tips? I confess I'm afraid of downvote snipers... – Victor F Aug 28 at 18:49
Don't be afraid, PCG is full of extremely nice people :) – Bad Cat Eye Aug 28 at 19:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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