Re-writing Built-in Reduce Functions

Using this model as a template, it is possible to write the full implementation of the built-in functions _sum and _count when working with the sales data and the standard map() function below:

function(doc, meta)
{
emit(meta.id, null);
}

The _count function returns a count of all the records for a given key.
Since argument for the reduce function contains an array of all the values for a given key, the length of the array needs to be returned in the reduce() function: