Create a gist now

Instantly share code, notes, and snippets.

@fmamud /ast.groovy
Last active Jul 11, 2016

What would you like to do?
TDC São Paulo - Palestra: Groovy como você nunca viu
package tdc.groovy.awesome
import groovy.transform.Immutable
import groovy.transform.ToString
// @TypeChecked
// @CompileStatic
@ToString
@Immutable
class Person {
String name
Integer id
}
def p = new Person(id: 1, name: 'Groovy')
p.id = 12 // exception
package tdc.groovy.awesome;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
class Boilerplate {
public static void main(String[] args) {
def names = ['Homer', 'Marge', 'Bart', 'Lisa']
println names
def b = new Boilerplate();
def shortNames = names.findAll { name -> name.size() <= 4 }
def shortNamesStream = names
.stream()
.filter {name -> name.length() <= 4}
.collect(Collectors.toList())
println shortNames
println shortNamesStream
}
}
package tdc.groovy.awesome
import groovy.json.JsonBuilder
import groovy.xml.MarkupBuilder
// MarkupBuilder
// JSON
def json = new JsonBuilder()
json.eventos {
evento {
name 'TDC 2016'
palestra 'Groovy'
}
}
println json
package tdc.groovy.awesome
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
// Operator overloading
// Closure Delegate (EmailSender) @DelegatesTo
// Category
class EmailSender {
def from(String from) { println "From $from"}
def to(String ...to) { println "To: ${to.join(',')}" }
def message(String msg) { println "Message: ${msg}"}
}
def email(@DelegatesTo(EmailSender) Closure cls) {
cls.delegate = new EmailSender()
cls()
}
@TypeChecked
@CompileStatic
def enviaEmail() {
email {
from 'dooms@groovy.com'
to 'john@brasil.com'
message 'Fala ai john!! Blza?!!'
}
}
package tdc.groovy.awesome
println 'http://www.google.com.br'.toURL().text
println 'ls -l'.execute().text
package tdc.groovy.awesome
//Optional (Typing, Semicolons, Parentheses, public & return keyword);
def hello(String saudacao) {
"Bem vindos ao " + saudacao
}
def nomes = ['Dooms', 'Tincas', 'John']
println nomes.class
hello "TDC 2016"
def map = [nome: 'Dooms', lang:'Groovy']
map.each { e ->
println "${e.key} ${e.value}"
}
(1..10).each { n ->
println n
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment