Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #40646
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2021-12-09 17:44 -0800 |
| References | (1 earlier) <93cb03fa-9811-4e19-8469-6e0a0b9cae19n@googlegroups.com> <f38be122-740c-448e-b9d4-3691bcad8964n@googlegroups.com> <692463e5-c0df-4a6f-b6ad-e1cb1c96c92cn@googlegroups.com> <b94ff525-ca41-4574-a77d-da1f7c37fbd2n@googlegroups.com> <a01b7437-266b-47a5-90b3-6818a59ee7c1n@googlegroups.com> |
| Message-ID | <54d48386-efba-4696-b99d-eaf8f7d152cdn@googlegroups.com> (permalink) |
| Subject | Re: sidetrack |
| From | luserdroog <luser.droog@gmail.com> |
On Thursday, December 9, 2021 at 7:39:22 PM UTC-6, luserdroog wrote:
> On Wednesday, December 8, 2021 at 11:38:30 PM UTC-6, Michael Haufe (TNO) wrote:
> > On Wednesday, December 8, 2021 at 6:23:12 PM UTC-6, luser...@gmail.com wrote:
> > > On Wednesday, December 8, 2021 at 6:17:07 PM UTC-6, luserdroog wrote:
> > >
> > > > function running_sum( a ){
> > > > var t = [];
> > > > a.reduce( (pre,cur)=>{t.push(pre+cur),pre+cur}
> > > , 0
> > > > );
> > > > return t;
> > > > }
> > > >
> > > > It's not a "one liner", but this is probably a bit more efficient AFAICT.
> > > > In APL, it's just `+\`.
> > > Need to use initial value zero (or prepend the original first element).
> > > I really think that's right, now.
> > functionally:
> >
> > const add = (left, right) => left + right
> >
> > const sum = (xs) => xs.reduce(add,0)
> >
> > sum([1,2,3,4,5]) // 15
> That does an APL reduce (coincidentally named) which looks like:
>
> +/
>
> But I'm after the subtly different function does a "scan" or piece-wise
> reduce. In APL, it's almost imperceptibly different:
>
> +\
>
> (There's a joke that APL\360 was named with the backslash character
> in there so the product name means "APL extends 360" if treated as
> an APL program. \ means "extend" if the left and right are values, but
> in the snippets under consideration \ means "scan" because the left
> (+) is a function and not a value. I will endeavor to stop talking about
> APL after this message.)
>
> To be more functional, I think it would look like this:
>
> Array.prototype.scan = function( f ){
> return this.map( (e,i,a)=>a.slice(0,i+1).reduce(f,0) );
dangitall.
> }
> const add = (left,right) => left + right
>
> function running_sum( a ){
> return [ 0, ...a.scan( add )];
> }
>
> (I forgot I also needed the result of running_sum() to start with a zero,
> in addition to the zero that gets fed to reduce() to ensure the first
> element of the input array shows up in the output array.)
Erhm. We don't need that zero in reduce() anymore. The map() takes
care of that.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-07 18:42 -0800
Re: sidetrack "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-12-08 13:03 -0800
Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-08 16:17 -0800
Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-08 16:23 -0800
Re: sidetrack "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-12-08 21:38 -0800
Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-09 17:39 -0800
Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-09 17:44 -0800
Re: sidetrack Scott Sauyet <scott@sauyet.com> - 2021-12-10 07:26 -0800
Re: sidetrack Elhwen Dico <elhwen.dicote@gmail.com> - 2021-12-10 16:30 +0100
Re: sidetrack Elhwen Dico <elhwen.dicote@gmail.com> - 2021-12-10 16:37 +0100
Re: sidetrack Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-12-11 01:56 +0100
Re: sidetrack Julio Di Egidio <julio@diegidio.name> - 2021-12-11 10:12 +0100
Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-09 19:22 -0800
csiph-web