My head nearly exploded looking at that. Map reduce is not a good pattern for application engineering, it's a backend big-unstructured-data pattern.
retrievePostsFor(me) would be so much easier to think about as well. Baking in getting all posts and filtering is just not clear unless you have a shallow micro-service code-base, embrace the cascade.
Finally wrap that up in a method
function totalLikes(user) {
return likes = retrievePostsFor(user)
.map(p => p.likes)
.reduce((sum, likes) => sum + likes, 0);
}
to anyone liking this, it works better if you just send in users. Then it doesn't matter where they come from, and anything with 0 likes just adds items to the list...
In-fact it doesn't need to be a user at all. Just something implementing a likeable interface, which has a method to retrieve likes.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My head nearly exploded looking at that. Map reduce is not a good pattern for application engineering, it's a backend big-unstructured-data pattern.
retrievePostsFor(me)
would be so much easier to think about as well. Baking in getting all posts and filtering is just not clear unless you have a shallow micro-service code-base, embrace the cascade.Finally wrap that up in a method
to anyone liking this, it works better if you just send in users. Then it doesn't matter where they come from, and anything with 0 likes just adds items to the list...
In-fact it doesn't need to be a user at all. Just something implementing a likeable interface, which has a method to retrieve likes.