implementation of map and reduce