# [Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin

Danilo J. S. Bellini danilo.bellini at gmail.com
Sat Apr 14 15:57:27 EDT 2018

```On 5 April 2018 at 13:52, Peter O'Connor <peter.ed.oconnor at gmail.com> wrote:

> I was thinking it would be nice to be able to encapsulate this common type
> of operation into a more compact comprehension.
>
> I propose a new "Reduce-Map" comprehension that allows us to write:
>
> signal = [math.sin(i*0.01) + random.normalvariate(0, 0.1) for i in range(1000)]
> smooth_signal = [average = (1-decay)*average + decay*x for x in signal from average=0.]
>
>
> def exponential_moving_average(signal: Iterable[float], decay: float, initial_value: float=0.):
>     average = initial_value
>     for xt in signal:
>         average = (1-decay)*average + decay*xt
>         yield average
>
> signal = [math.sin(i*0.01) + random.normalvariate(0, 0.1) for i in range(1000)]
> smooth_signal = list(exponential_moving_average(signal, decay=0.05))
>
> I wrote in this mail list the very same proposal some time ago. I was
trying to let the scan higher order function (itertools.accumulate with a
lambda, or what was done in the example above) fit into a simpler list
comprehension.

As a result, I wrote this project, that adds the "scan" feature to Python
comprehensions using a decorator that performs bytecode manipulation (and
it had to fit in with a valid Python syntax): https://github.com/danilobelli
ni/pyscanprev

In that GitHub page I've wrote several examples and a rationale on why this
would be useful.

--
Danilo J. S. Bellini
---------------
"*It is not our business to set up prohibitions, but to arrive at
conventions.*" (R. Carnap)
-------------- next part --------------
An HTML attachment was scrubbed...