implement mapReduce function