%% Add Multiple Key/Value Pairs to KeyValueStore
% Use add and addmulti in map and reduce functions to pass data into the intermediate and final KeyValueStore. This example uses identity map and reduce functions that pass the inputs straight through to the output. The map and reduce functions are listed at the end of the example as local functions.

inds = tabularTextDatastore('airlinesmall.csv','SelectedVariableNames',...
    {'ArrDelay','DepDelay'},'TreatAsMissing','NA');
preview(inds)
outds = mapreduce(inds,@myMapper,@myReducer,mapreducer(0));
readall(outds)

%
%% Local Functions

function myMapper(data,info,intermKV)
    addmulti(intermKV,{'ArrDelay' 'DepDelay'},{data.ArrDelay data.DepDelay});
end

function myReducer(key,intermValIter,outKV)
    data = getnext(intermValIter);
    while hasnext(intermValIter)
        data = [data; getnext(intermValIter)];
    end
    add(outKV,key,data);
end

% Copyright 2019 The MathWorks, Inc.