Skip to content

Instantly share code, notes, and snippets.

@csauve
Created December 8, 2014 22:10
Show Gist options
  • Save csauve/43bf117a31dca9808228 to your computer and use it in GitHub Desktop.
Save csauve/43bf117a31dca9808228 to your computer and use it in GitHub Desktop.
Simple map-reduce in Groovy
def mapReduce = { collection, mapper, reducer ->
collection.groupBy(mapper)
.collectEntries { k, v ->
[k, reducer(v)]
}
}
mapReduce([1, 6, 3, 7, 3, 9, 1, 2, 10, 9, 5, 8, 4],
{ it % 2 ? "odd" : "even" },
{ it.size })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment