Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Created June 26, 2013 12:24
Show Gist options
  • Select an option

  • Save michalbcz/5866983 to your computer and use it in GitHub Desktop.

Select an option

Save michalbcz/5866983 to your computer and use it in GitHub Desktop.
groovy - Map#collectWithIndex implementation
Map.metaClass.collectWithIndex = { yield ->
def collected = []
delegate.eachWithIndex { key, value, index ->
collected << yield(key, value, index)
}
return collected
}
assert [a: 1, b: 1, c: 1].collectWithIndex { key, value, index -> key + value + index } == ["a10", "b11", "c12"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment