I'm trying to write a version of cumulative sum in python using the reduce function. Here is my code so far:
from functools import reduce
def my_cum_sum(arg):
return reduce(lambda a, x: (a.append(a[-1] + x)) if len(a) > 0 else a.append(x), arg, [])
assert(my_cum_sum([1, 1, 1, 1]) == [1, 2, 3, 4]))
But the problem is that in my lambda function, python doesn't know that a (my accumulator parameter) is a list object, and that I want my reduce function to return a list. In other functional programming languages, it might ask me to specify the type of a and x. But I'm new to python, and haven't quite figured out how it handles types and stuff. What is the pythonic way of solving this problem?