Skip to content

Instantly share code, notes, and snippets.

@edysegura
Last active June 23, 2016 20:03
Show Gist options
  • Save edysegura/8e3621f0ca886df7d96a to your computer and use it in GitHub Desktop.
Save edysegura/8e3621f0ca886df7d96a to your computer and use it in GitHub Desktop.
Groovy's Magic
def out = System.out.&println
out "Haaa!" //Haa
def data = [key1:'Value1', key2:'Value2']
def key = 'key2'
print data."$key" //output: value2
//java
Integer n = 10;
List list = new ArrayList<Integer>();
for(int i=0; i<n; i++) {
list.add(Math.pow(2, i));
}
System.out.println(list);
//output [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
//groovy
(0..<10).collect { 2 ** it }
//output [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
def test = [
[id:79, siteName:"Site 1"],
[id:80, siteName:"Site 2"],
[id:81, siteName:"Site 3"]
]
println test*.id << 78 //output: [79, 80, 81, 78]
//java
for(int i=-3; i<4; i++) {
System.out.println(i);
}
//groovy
(-3..3).each { print it }
//output -3-2-10123
('a'..'z').each { print it }
//output abcdefghijklmnopqrstuvwxyz
10.times {
println "Valeu!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment