Skip to content

Instantly share code, notes, and snippets.

@neumino
Last active December 30, 2015 01:49
Show Gist options
  • Save neumino/7758242 to your computer and use it in GitHub Desktop.
Save neumino/7758242 to your computer and use it in GitHub Desktop.
r.table('marvel').groupedMapReduce(
function(hero) { return hero('weightClass'), // group function
function(hero) { // map function
return {
strength: hero('strength'),
height: hero('height'),
count: 1
}
},
function(left, right) { // reduce function
return {
strength: left('strength').add(right('strength')), // sum the strength
height: left('height').add(right('height'), // sum the height
count: left('count').add(right('count')) // count
}
}
).map( function(weightClass) { // compute the average
return {
group: weightClass('group')
avgStrength: weightClass('reduction')('strength').div(weightClass('reduction')('count')),
avgHeight: weightClass('reduction')('height').div(weightClass('reduction')('count'))
}
}).run(conn, callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment