Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Created May 20, 2012 10:47
Show Gist options
  • Save michalbcz/2757630 to your computer and use it in GitHub Desktop.
Save michalbcz/2757630 to your computer and use it in GitHub Desktop.
groovy - when you're missing collectWithIndex method...
List.metaClass.collectWithIndex = { yield ->
def collected = []
delegate.eachWithIndex { listItem, index ->
collected << yield(listItem, index)
}
return collected
}
assert [1, 1, 1, 1, 1].collectWithIndex { it, index -> it + index } == [1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment