The Map Phase

Step 1: The Map Phase

MapReduce starts by transforming input records into key/value pairs. The map function is called once per input and emits zero or more pairs; the framework flattens all of them into a single stream. For word count, mapping the line "a b" emits [("a", 1), ("b", 1)]: one pair per word.

Build the map phase:

  • map_phase(inputs, mapper): call mapper(item) for each input (each returns a list of pairs) and return all the pairs concatenated in order.
Hints
Starter code
def map_phase(inputs, mapper):
    """Apply mapper to each input and flatten the emitted pairs."""
    pairs = []
    # TODO: walk the inputs left to right and call the mapper on each one. Every
    # call hands back a list of pairs, so grow the pairs accumulator with all of
    # its entries rather than nesting a list inside it, then return the flat
    # accumulator. No inputs means an empty list comes back.
    pass


def wc_map(line):
    """Word-count mapper: one (word, 1) pair per word."""
    return [(w, 1) for w in line.split()]
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.