14
\$\begingroup\$

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.

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

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()
\$\endgroup\$
  • \$\begingroup\$ if on something listish the *.X spread operator can often be written as .X \$\endgroup\$ – cfrick Jul 7 '14 at 20:50
3
\$\begingroup\$

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)
\$\endgroup\$
2
\$\begingroup\$

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)

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

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'))
\$\endgroup\$
  • 1
    \$\begingroup\$ Would this question accept Grails golfing tips? I confess I'm afraid of downvote snipers... \$\endgroup\$ – Victor F Aug 28 '15 at 18:49
  • 2
    \$\begingroup\$ Don't be afraid, PCG is full of extremely nice people :) \$\endgroup\$ – Beta Decay Aug 28 '15 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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