Skip to content

Instantly share code, notes, and snippets.

@lakemove
Created June 20, 2017 15:54
Show Gist options
  • Save lakemove/a9a116b462c32fa20558fde10297880e to your computer and use it in GitHub Desktop.
Save lakemove/a9a116b462c32fa20558fde10297880e to your computer and use it in GitHub Desktop.
one liner in groovy
//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