Tips For Golfing In Groovy

14

1

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.

Ant's

Posted 2011-06-17T09:46:42.323

Reputation: 281

1Well this isn't very popular, is it :-( – Armand – 2011-11-29T20:01:58.390

@Alison : I feel Bad, as there is no Golf tip for Groovy in this site :( – Ant's – 2011-11-30T05:25:44.473

2Do you have any tips to start us off? – Armand – 2011-11-30T11:50:23.900

Answers

5

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()

Fels

Posted 2011-06-17T09:46:42.323

Reputation: 488

if on something listish the *.X spread operator can often be written as .X – cfrick – 2014-07-07T20:50:41.183

3

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:

''.metaClass.r<<{i->(int)Math.random()*i}
''.r(12)

Tip golfing edit:

0.metaClass.r<<{i->(int)Math.random()*i}
0.r(12)

Or, you know:

r={(int)Math.random()*it}
r(12)

codeporn

Posted 2011-06-17T09:46:42.323

Reputation: 261

2

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)

cfrick

Posted 2011-06-17T09:46:42.323

Reputation: 313

1'ABC XYZ'as Set is one character shorter and works in most of the same places – Marty Neal – 2018-06-14T15:01:08.933

2

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'))

Victor F

Posted 2011-06-17T09:46:42.323

Reputation: 121

1Would this question accept Grails golfing tips? I confess I'm afraid of downvote snipers... – Victor F – 2015-08-28T18:49:08.110

2Don't be afraid, PCG is full of extremely nice people :) – Beta Decay – 2015-08-28T19:09:11.920