summaryrefslogtreecommitdiff
path: root/game/python-extra/utils/math.py
blob: eaae1877396996a964b08b4507eff3d39b23cc22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import collections
import operator

# py3 doesn't include reduce as a builtin
try:
    reduce
except NameError:
    from functools import reduce


def product(sequence, initial=1):
    """like the built-in sum, but for multiplication."""
    if not isinstance(sequence, collections.Iterable):
        raise TypeError("'{}' object is not iterable".format(type(sequence).__name__))

    return reduce(operator.mul, sequence, initial)