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

2018-04-05 Thread Serhiy Storchaka
05.04.18 19:52, Peter O'Connor пише: I propose a new "Reduce-Map" comprehension that allows us to write: signal = [math.sin(i*0.01) + random.normalvariate(0, 0.1)for iin range(1000)] smooth_signal = [average = (1-decay)*average + decay*xfor xin signalfrom average=0.] Using currently supported

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

2018-04-05 Thread Steven D'Aprano
On Fri, Apr 06, 2018 at 11:02:30AM +1000, Chris Angelico wrote: > On Fri, Apr 6, 2018 at 10:37 AM, Steven D'Aprano wrote: [...] > > All we need now is a way to feed in the initial value for average. And > > that could be as trival as assigning a local name for it: > > > > average = 0 > > > >

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

2018-04-05 Thread Steven D'Aprano
On Thu, Apr 05, 2018 at 12:52:17PM -0400, Peter O'Connor wrote: > 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 > f

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

2018-04-05 Thread Ethan Furman
On 04/05/2018 05:37 PM, Steven D'Aprano wrote: On Thu, Apr 05, 2018 at 05:31:41PM -0700, Ethan Furman wrote: [snip unkind words] Be fair. Strip out the last "from average = 0" and we have little that isn't either in Python or is currently being proposed elsewhere. Ugh. Thanks for remindin

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

2018-04-05 Thread Chris Angelico
On Fri, Apr 6, 2018 at 10:37 AM, Steven D'Aprano wrote: > On Thu, Apr 05, 2018 at 05:31:41PM -0700, Ethan Furman wrote: >> On 04/05/2018 03:24 PM, Peter O'Connor wrote: >> >> >Well, whether you factor out the loop-function is a separate issue. Lets >> >say we do: >> > >> > smooth_signal = [av

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

2018-04-05 Thread Steven D'Aprano
On Fri, Apr 06, 2018 at 10:29:19AM +1000, Steven D'Aprano wrote: > - That you call it "MapReduce" while apparently doing something > different from what other people call MapReduce: Actually, no you don't -- you call it "Reduce-Map". Sorry, my mistake. -- Steve __

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

2018-04-05 Thread Steven D'Aprano
On Thu, Apr 05, 2018 at 05:31:41PM -0700, Ethan Furman wrote: > On 04/05/2018 03:24 PM, Peter O'Connor wrote: > > >Well, whether you factor out the loop-function is a separate issue. Lets > >say we do: > > > > smooth_signal = [average = compute_avg(average, x) for x in signal > > from a

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

2018-04-05 Thread Steven D'Aprano
On Thu, Apr 05, 2018 at 06:24:25PM -0400, Peter O'Connor wrote: > Well, whether you factor out the loop-function is a separate issue. Lets > say we do: > > smooth_signal = [average = compute_avg(average, x) for x in signal from > average=0] > > Is just as readable and maintainable as yo

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

2018-04-05 Thread Ethan Furman
On 04/05/2018 03:24 PM, Peter O'Connor wrote: Well, whether you factor out the loop-function is a separate issue. Lets say we do: smooth_signal = [average = compute_avg(average, x) for x in signal from average=0] Is just as readable and maintainable as your expanded version, but saves

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

2018-04-05 Thread David Mertz
On Thu, Apr 5, 2018, 5:32 PM Peter O'Connor wrote: > I find this a bit awkward, and maintain that it would be nice to have this > as a built-in language construct to do this natively. You have to admit: > > smooth_signal = [average = (1-decay)*average + decay*x for x in signal > from average

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

2018-04-05 Thread Peter O'Connor
Well, whether you factor out the loop-function is a separate issue. Lets say we do: smooth_signal = [average = compute_avg(average, x) for x in signal from average=0] Is just as readable and maintainable as your expanded version, but saves 4 lines of code. What's not to love? On Thu, A

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

2018-04-05 Thread Paul Moore
On 5 April 2018 at 22:26, Peter O'Connor wrote: > I find this a bit awkward, and maintain that it would be nice to have this > as a built-in language construct to do this natively. You have to admit: > > smooth_signal = [average = (1-decay)*average + decay*x for x in signal > from average=0.]

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

2018-04-05 Thread Peter O'Connor
Ah, that's nice, I didn't know that itertools.accumulate now has an optional "func" parameter. Although to get the exact same behaviour (output the same length as input) you'd actually have to do: smooth_signal = itertools.islice(itertools.accumulate([initial_average] + signal, compute_avg), 1

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

2018-04-05 Thread Clint Hepner
> On 2018 Apr 5 , at 12:52 p, Peter O'Connor wrote: > > Dear all, > > In Python, I often find myself building lists where each element depends on > the last. This generally means making a for-loop, create an initial list, > and appending to it in the loop, or creating a generator-function.

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

2018-04-05 Thread Ethan Furman
On 04/05/2018 09:52 AM, Peter O'Connor wrote: [snip html code snippets] Please don't use html markup. The code was very difficult to read. -- ~Ethan~ ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/py

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

2018-04-05 Thread Rhodri James
On 05/04/18 17:52, Peter O'Connor wrote: Dear all, In Python, I often find myself building lists where each element depends on the last. This generally means making a for-loop, create an initial list, and appending to it in the loop, or creating a generator-function. Both of these feel more ve

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

2018-04-05 Thread Peter O'Connor
Dear all, In Python, I often find myself building lists where each element depends on the last. This generally means making a for-loop, create an initial list, and appending to it in the loop, or creating a generator-function. Both of these feel more verbose than necessary. I was thinking it wo