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.
Tell me more
×
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 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:
|
||||
|
|
|
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:
Edit Conversions:
|
||||
|
|