range-based reduce algorithm