Created
February 8, 2016 05:40
-
-
Save uolter/26f4e645f47fb5aa088a to your computer and use it in GitHub Desktop.
Map reduce engine emulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'uolter' | |
""" | |
Defines a single function, map_reduce, which takes an input | |
dictionary i and applies the user-defined function mapper to each | |
(input_key,input_value) pair, producing a list of intermediate | |
keys and intermediate values. Repeated intermediate keys then | |
have their values grouped into a list, and the user-defined | |
function reducer is applied to the intermediate key and list of | |
intermediate values. The results are returned as a list. | |
""" | |
import itertools | |
def map_reduce(i, mapper, reducer, debug=False): | |
intermediate = [] | |
for (key, value) in i.items(): | |
map_out = mapper(key, value) | |
if debug: | |
print(map_out) | |
intermediate.extend(map_out) | |
groups = {} | |
for key, group in itertools.groupby(sorted(intermediate), lambda x: x[0]): | |
groups[key] = list([y for x, y in group]) | |
return [reducer(intermediate_key, groups[intermediate_key]) for intermediate_key in groups] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment