Created
June 20, 2017 15:54
-
-
Save lakemove/a9a116b462c32fa20558fde10297880e to your computer and use it in GitHub Desktop.
one liner in groovy
This file contains 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
//turn list into map | |
assert [1:2,3:4] == [[1,2],[3,4],[1,2]].collectEntries {it} //{[it[0],it[1]]} | |
//sublist from nth to end | |
assert [3,4,5,6] == [1,2,3,4,5,6][2..-1] | |
assert [3,4,5,6] == [1,2,3,4,5,6][2, 3..-1] | |
//string to number | |
assert 1.2 == "1.2" as double | |
//if x exists then x else default | |
assert "hello" == System.properties["hello"] ?: "hello" | |
//remove null from list | |
assert [1,"a"] == [1,null,"a"] - null | |
//remove duplicate entries | |
assert [1,2] == [1,1,2].unique() | |
//regex matcher as collection | |
assert [1,2,3] == ("1 2 3" =~ /\d/).collect{it as Integer} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment