#!/usr/bin/env python

"""
CS373: Quiz #17 (5 pts)
"""

""" ----------------------------------------------------------------------
1. Define my_reduce(), such that it emulates reduce().
   It MUST NOT use reduce().
   No need to check the preconditions.
   (4 pts)
"""

import operator

"""
param  a binary function
param  an iterable (NOT indexable)
param  a seed
return the result of repeatedly invoking the binary function on the seed
       and the elements of the iterable from left to right
"""
def my_reduce (bf, a, v) :
    for w in a :
        v = bf(v, w)
    return v

def test (f) :
    a = [2, 3, 4]
    assert f(operator.add, a, 0) ==  9
    assert f(operator.sub, a, 0) == -9
    assert f(operator.mul, a, 1) == 24

print "Reduce.py"

test(   reduce)
test(my_reduce)

print "Done."
