Вход | Регистрация

Решение на втора задача

  • Ето примерно решение:

    def reduce(func, seq, init = None):
        if init is None and seq:
            init, *seq = seq
        for x in seq:
            init = func(init, x)
        return init
    
    def groupby(func, seq):
        groups = {}
        for item in seq:
            groups.setdefault(func(item), []).append(item)
        return groups
    
    def construct(functions, *args):
        return [f(*args) for f in functions]
    
    def curry(func, *args, **kwargs):
        def curried(*newargs, **newkwargs):
            merged_kwargs = kwargs.copy()
            merged_kwargs.update(newkwargs)
            return func(*(args + newargs), **merged_kwargs)
        return curried
    
    def compose(func, *functions):
        return lambda *args, **kwargs: reduce(lambda value, f: f(value), functions, func(*args, **kwargs))
    06.04.2009