Last active
June 23, 2016 20:03
-
-
Save edysegura/8e3621f0ca886df7d96a to your computer and use it in GitHub Desktop.
Groovy's Magic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def out = System.out.&println | |
out "Haaa!" //Haa |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def data = [key1:'Value1', key2:'Value2'] | |
def key = 'key2' | |
print data."$key" //output: value2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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