%% MapReduce Function Files
%% Identity Map Function
% A map function that simply returns what mapreduce passes to it is called an identity mapper. An identity mapper is useful to take advantage of the grouping of values by unique key before doing calculations in the reduce function. The identityMapper.m mapper file is one of the mappers used in the example file TSQRMapReduceExample.m.

type identityMapper.m

%
%% Simple Map Function
% One of the simplest examples of a nonidentity mapper is maxArrivalDelayMapper.m, which is the mapper for the example file MaxMapReduceExample.m. For each chunk of input data, this mapper calculates the maximum arrival delay and adds a key-value pair to the intermediate KeyValueStore.

type maxArrivalDelayMapper.m

%
%% Advanced Map Function
% A more advanced example of a mapper is statsByGroupMapper.m, which is the mapper for the example file StatisticsByGroupMapReduceExample.m. This mapper uses a nested function to calculate several statistical quantities (count, mean, variance, and so on) for each chunk of input data, and then adds several key-value pairs to the intermediate KeyValueStore object. Also, this mapper uses four input arguments, whereas mapreduce only accepts a map function with three input arguments. To get around this, pass in the extra parameter using an anonymous function during the call to mapreduce, as outlined in the example.

type statsByGroupMapper.m

%
%% Simple Reduce Function
% One of the simplest examples of a reducer is maxArrivalDelayReducer.m, which is the reducer for the example file MaxMapReduceExample.m. The map function in this example finds the maximum arrival delay in each chunk of input data. Then the reduce function finishes the task by finding the single maximum value among all of the intermediate maxima. To find the maximum value, the reducer scrolls through the values in the ValueIterator object and compares each value to the current maximum. mapreduce only calls this reducer function once, since the mapper adds a single unique key to the intermediate KeyValueStore object. The reduce function adds a single key-value pair to the output.

type maxArrivalDelayReducer.m

%
%% Advanced Reduce Function
% A more advanced example of a reducer is statsByGroupReducer.m, which is the reducer for the example file StatisticsByGroupMapReduceExample.m. The map function in this example groups the data in each input using an extra parameter (airline carrier, month, and so on), and then calculates several statistical quantities for each group of data. The reduce function finishes the task by retrieving the statistical quantities and concatenating them into long vectors, and then using the vectors to calculate the final statistical quantities for count, mean, variance, skewness, and kurtosis. The reducer stores these values as fields in a structure, so that each unique key has a structure of statistical quantities in the output.

type statsByGroupReducer.m

% Copyright 2015 The MathWorks, Inc.