Skip to content

Instantly share code, notes, and snippets.

@urbansky
Last active August 30, 2017 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urbansky/01591885b6b71d061ddf323a6d3246ce to your computer and use it in GitHub Desktop.
Save urbansky/01591885b6b71d061ddf323a6d3246ce to your computer and use it in GitHub Desktop.
Methods in groovy collections
assert "Found 2" == [1,2,3].findResult { it > 1 ? "Found $it" : null } // Return if not null
assert ["Found 2", "Found 3"] == [1,2,3].findResults { it > 1 ? "Found $it" : null }
assert 1*1*2*3 == [1,2,3].inject(1) { acc, val -> acc * val }
assert [2,4,6] == [1,2,3].collect { it * 2 }
assert [2,4] == [1,2,3,4].findAll { it % 2 == 0 }
//See API: http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Collection.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment