# -*- coding: utf-8 -*-
# Copyright (C) 2008-2010, Luis Pedro Coelho <lpc@cmu.edu>
# vim: set ts=4 sts=4 sw=4 expandtab smartindent:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

from __future__ import division
from jug import Task
from itertools import chain

def _jug_map(mapper, inputs):
     return map(mapper, inputs)
def _jug_reduce(reducer, inputs):
    return reduce(reducer, chain(inputs))

def mapreduce(reducer, mapper, inputs, map_step=4, reduce_step=8):
    '''
    task = mapreduce(reducer, mapper, inputs, map_step=4, reduce_step=8)

    Create a task that does roughly the following::

        reduce(reducer, map(mapper, inputs))

    Roughly because the order of operations might be different. In particular,
    `reducer` should be a true `reducer` functions (i.e., cumutative and
    associative).

    Parameters
    ----------
      reducer : associative, comutative function mapping
                    [Y_0,Y_1...] -> Y
      mapper : function from X -> Y
      inputs : list of X
    
      map_step : Number of mapping operations to do in one go.
                 This is what defines an inner task. (default: 4)
      reduce_step : Number of reduce operations to do in one go.
                 (default: 8)
    Returns
    -------
      A task
    '''
    def _task_map(jug_op, func, args, step):
        nsteps = len(args)//step + bool(len(args) % step)
        return [Task(jug_op, func, args[i*step:(i+1)*step]) for i in xrange(nsteps)]
    reducers = [Task(_jug_reduce, reducer, mapped) for mapped in _task_map(_jug_map, mapper, inputs, map_step)]
    while len(reducers) > 1:
        reducers = _task_map(_jug_reduce, reducer, reducers, reduce_step)
    return reducers[0]

