Skip to content

Instantly share code, notes, and snippets.

@George1
Forked from michalbcz/gist:2757630
Created September 27, 2012 13:15
Show Gist options
  • Save George1/3793925 to your computer and use it in GitHub Desktop.
Save George1/3793925 to your computer and use it in GitHub Desktop.
groovy - when you're missing collectWithIndex method...
Collection.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