reduce_()
Reduce implementation for test compatibility.
Implementation
Args: iterable: The collection to reduce func: The reducer function initial: Initial value Returns: The reduced value
Example
reduce_([1, 2, 3], lambda x, y: x + y)
Expected output: 6
Source Code
def reduce_(iterable, func, initial=None):
if initial is None:
iterator = iter(iterable)
try:
initial = next(iterator)
except StopIteration:
raise TypeError("reduce() of empty sequence with no initial value")
iterable = iterator
result = initial
for item in iterable:
result = func(result, item)
return result