More Related Content Similar to Groovy Fly Through (20) Groovy Fly Through11. What does it look like?
class First {
static void main(String[] args) {
println "You just gave me " + args[0]
}
}
13. Many Variants
class Think {
String value
def getMeaning() {
"You just gave me ${value}."
}
static main(args) {
def first = args? args[0] : "nothing"
println new Think(value:first).meaning
}
}
17. Ceremony
public class Item {
private String name;
private String description;
public Item() {}
public String getName() { return name; }
public String setName(String name) {
this.name = name;
}
public String getDescription() { return description; }
public void setDescription(String description) {
this.description = description;
}
}
23. Type Hybrid
Dynamic refs are declared with def.
Typed refs are auto-cast & converted:
String v = 123
v.bytes[-1] == '3'
29. Slice
def l = ['a', 'b', 'c', 'd']
assert l[1..-2] == ['b', 'c']
30. Map Expandos
Map map = ['a': 1, b: 2]
assert map['a'] == 1
assert map.b == 2
map.c = 3
map['d'] = 3
assert map['c'] == map.d
32. Sift and Transform
def values = ["1", "2", "12", "123"]
def some = values.findAll { it.size() > 1 }
def ints = some.collect { (it as int) * 2 }
assert ints == [24, 246]
34. Xml...
def slurper = new XmlSlurper()
def html = slurper.parse(new File("/some.xhtml"))
html.body.'*'.each {
println it.'@id'
}
46. The Power of Prototyping
"You can use Groovy as
an exploratory language
for functionality spikes."