
# The reduce() (1) combines the first two elements of a list, 
# then (2) that result with the third element of the list, and
#      (3) that result with the fourth, and so on, 
#
# It will produce a single result


# importing functools for reduce()
import functools

def multiply(x, y): return x * y

xs = [1, 2, 3, 4]

x_product = functools.reduce(multiply, xs) 		# = 1 * 2 * 3 * 4 = 24
print(x_product)

# Shorter:

x_product = functools.reduce(lambda a,b: a*b, xs)
print(x_product)


# ####################################################
# You can specialize the function with partial again:
# ####################################################

list_product = functools.partial(functools.reduce, multiply)

x_product = list_product(xs)
print(x_product)

