Last active
December 30, 2015 01:49
-
-
Save neumino/7758242 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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