Created
August 25, 2013 23:23
-
-
Save peysal/6336930 to your computer and use it in GitHub Desktop.
groovy introduction closure
1) default it parameter
2) parameter more than 1
3) passing closure to method
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 myCodeBlock1 = {println it; it} //closure with 1 parameter | |
| myCodeBlock1("test") | |
| assert myCodeBlock1("test") == "test" | |
| def myCodeBlock2 = {a,b -> a+b} | |
| assert myCodeBlock2(1,2) == 3 | |
| def list = ['a','b','c','d'] | |
| def newList = [] | |
| def clos = { it.toUpperCase() } //accept 1 argument | |
| list.collect( newList, clos ) //passing closure as part of parameter | |
| assert newList == ["A", "B", "C", "D"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment