map(fun, iter) --> [ fun(x) for x in iter ]
filter(fun, iter) --> [ x for x in iter if fun(x) ]
reduce(bifun, iter, initial) --> ???
edit: I'll also add that this is made worse by the fact that itertools lacks a sliding window function. map(fun, iter) --> [ fun(x) for x in iter ]
filter(fun, iter) --> [ x for x in iter if fun(x) ]
reduce(bifun, iter, initial) --> ???
edit: I'll also add that this is made worse by the fact that itertools lacks a sliding window function. last = initial
for value in iter:
last = byfun(last, value)
However, I don't find myself writing that frequently. accum = initial
series = [accum := bifun(accum, value) for value in seq]
# result = series[-1]
To be honest, this is the first time I've used the walrus operator.(Seriously, I like that.)
x = 0
deque((x := x + 1 for _ in range(10)), maxlen=0)
print(x)
>> 10
Using deque to consume generators is an old trick documented in the itertools recipes: https://docs.python.org/3/library/itertools.html#itertools-r...What types of situations do you use reduce for?
Also you don't need map. You can use list comprehension to replace map. But list comprehensions can't replace reduce.
Technically reduce can also replace map. The only primitive you need to do all your iterations is technically just reduce.
If you want to get more elegant than you don't even need reduce. You can eliminate all redundant primitives and use functions themselves for iteration. It's Just recursion.
Coworkers don't hate it it's more readable in general to split between map and reduce and forego looping.
It's less readable to use reduce for all looping even though technically you can.
We have `itertools.pairwise()` for making a sliding window of pairs, but yes, there is no function that accepts an arbitrary number of elements.