Re: [Python-ideas] Reduce/fold and scan with generator expressions and comprehensions

2016-11-13 Thread Danilo J. S. Bellini
2016-11-06 23:27 GMT-02:00 Wes Turner :

> - So, IIUC, for recursive list comprehensions
>   - "prev" = x_(n-1)
>   - there is a need to define an initial value
> - chain([1000], [...])
>   - sometimes, we actually need window function
> - __[0] = x_(n-1)
> - __[1] = x_(n-2)  # this
> - __[-1] = x_(n-2)  # or this
> - this can be accomplished with dequeue
>   - __= dequeue([1000], maxlen)
> - for recursive list comprehensions, we'd want to bind e.g. __ to a
> dequeue
>
> [f(__[0], x) for x in y with __ = dequeue((1000,), 1)]
>

If I understood correctly, that's an alternative to get a general recursive
list comprehension with a syntax like:

[f(hist, x) for x in y with hist = deque([start_values], size)]

You're trying to solve the output lag/window problem using a circular queue
with random/indexed access to its values (a collections.deque instance).
You're using "with" instead of "from" to distinguish it from my first
proposal.

That's not a scan anymore, but something more general. Some information can
be removed from that syntax, for example the size can be defined to be the
starting iterable/memory/history data size, and the deque can be something
internal. Also, using the negative indices would be more explicit as
hist[-1] would be the previous iteration result, hist[-2] would be its
former result and so on. The syntax would be:

[func(hist, target) for target in iterable with hist = start_iterable]

i.e., this idea is about a new "with hist = start_iterable" at the end (or "
from" instead of "with"). The resulting list size would be
len(list(start_iterable))
+ len(list(iterable)). As a generator instead of a list, that can be
implemented as this "windowed scan" generator function:

>>> import collections
>>> def wscan(func, iterable, start_iterable):
... pre_hist = []
... for item in start_iterable:
... yield item
... pre_hist.append(item)
... hist = collections.deque(pre_hist, len(pre_hist))
... for target in iterable:
... item = func(hist, target)
... yield item
... hist.append(item)

The Fibonacci example would be written as:

>>> list(wscan(lambda fibs, unused: fibs[-1] + fibs[-2], range(10), [0, 1]))
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

With the "windowed scan" syntax proposal, it would become:

>>> [fibs[-1] + fibs[-2] for unused in range(10) with fibs = [0, 1]]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

Or:

>>> [fibs[-1] + fibs[-2] for unused in range(10) from fibs = [0, 1]]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

-- 
Danilo J. S. Bellini
---
"*It is not our business to set up prohibitions, but to arrive at
conventions.*" (R. Carnap)
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Re: [Python-ideas] Reverse assignment operators (=+, =-, =*, =/, =//, =**, =%)

2016-11-13 Thread Mikhail V
On 12 November 2016 at 21:08, João Matos  wrote:

> What I would like to propose is the creation of the reverse:
> a =+ c is the same as a = c + a
> a =- c is the same as a = c - a
> a =* c is the same as a = c * a
> a =/ c is the same as a = c / a
> a =// c is the same as a = c // a
> a =% c is the same as a = c % a
> a =** c is the same as a = c ** a

A good syntax example:

a = sum (a, c)
a = mul (a, c)
a = div (a, c)

Another good syntax, I'm not a fan of, but at least intuitive and
learnt in school:

a = a + c
a = a * c
a = a / c

Bad syntax, not readable:

a += c
a -= c
a *= c

And your proposal: slightly different looking, but as bad
as above, at least I don't see any improvement of the look, actually
it makes even harder to see the beginning of the right part of assignment,
so IMO it is merely worsening of already bad syntax.
As for me, I would even prohibit all these +=  for the sake of readability.

Mikhail
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/