Implement reduce function.