Is there a better way to do the running_sum()? I was trying to
do an APL "scan" and doing a reduce() while building the result
backwards was the best I could come up with.
function running_sum( a ){
var t = [];
a.reduce( (pre,cur)=>{t.push(pre+cur),pre+cur}
);
return t;
}
It's not a "one liner", but this is probably a bit more efficient AFAICT.
In APL, it's just `+\`.
On Tuesday, December 7, 2021 at 8:42:11 PM UTC-6, luser...@gmail.com wrote:
Is there a better way to do the running_sum()? I was trying toA `reduceRight` method exists on arrays:
do an APL "scan" and doing a reduce() while building the result
backwards was the best I could come up with.
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight>
On Wednesday, December 8, 2021 at 6:17:07 PM UTC-6, luserdroog wrote:
function running_sum( a ){, 0
var t = [];
a.reduce( (pre,cur)=>{t.push(pre+cur),pre+cur}
);
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.
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 ){, 0
var t = [];
a.reduce( (pre,cur)=>{t.push(pre+cur),pre+cur}
);
return t;
}
functionally: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.
const add = (left, right) => left + right
const sum = (xs) => xs.reduce(add,0)
sum([1,2,3,4,5]) // 15
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 ){, 0
var t = [];
a.reduce( (pre,cur)=>{t.push(pre+cur),pre+cur}
);
return t;
}
functionally:It's not a "one liner", but this is probably a bit more efficient AFAICT.Need to use initial value zero (or prepend the original first element).
In APL, it's just `+\`.
I really think that's right, now.
const add = (left, right) => left + right
const sum = (xs) => xs.reduce(add,0)
sum([1,2,3,4,5]) // 15That 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) );
}
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.)
I'm making some progress at reconceiving my pianoroll app
to be a more coherent object based design. But also kinda
not making progress on it. I doodled up a sketch of the overall
structure of the MVC example but instead of working on that,
here's a "robust" function for decoding note names from letters
and producing a MIDI note number.
It uses two minor tetrachords to construct the intervals of an
aeolian scale then takes a running sum to get "fretstops" for
each of the scale degrees. Then you index the resulting array
with 0 for 'a', 1 for 'b' etc, a little more twiddling and poof kazow
presto! The desired number comes out the other end.
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.
But I'm after the subtly different function does a "scan" or piece-wise reduce. In APL, it's almost imperceptibly different:
[ ...]
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) );
}
const add = (left,right) => left + right
function running_sum( a ){
return [ 0, ...a.scan( add )];
}
function running_sum(a: number[]) {
return a.reduce(
(acc: number, current: number) => acc + current,
0);
}
console.log(running_sum([1, 2, 3]));
console.log(running_sum([]));
Le 09/12/2021 à 01:23, luserdroog a écrit :
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.
[top post]
Elhwen Dico wrote:
[top post]
Bats are supposed to post next door.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 460 |
Nodes: | 16 (2 / 14) |
Uptime: | 18:50:35 |
Calls: | 9,355 |
Files: | 13,541 |
Messages: | 6,083,724 |