Re: Re: Additional Math functions
While Math.sum() and Math.mean() currently don't exist, they can easily be polyfilled: See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Sum_all_the_values_of_an_array for summarizing the values of an array and the following code for building the average of the array values: let arr = [0, 1, 2, 3]; let total = arr.reduce(function(a, b) { return a + b; }); let mean = total / arr.length; Calculating the variance and standard deviation would require a bit more code, though are also easy to polyfill. Non-the-less I can see the need for having standard functions for this. Sebastian On 2 October 2015 at 00:55, Eli Perelmanwrote: > In case it didn't come across, this is the thread I'm reviving: > > https://mail.mozilla.org/pipermail/es-discuss/2015-April/042732.html > > Eli Perelman > Mozilla > > On Thu, Oct 1, 2015 at 5:51 PM, Eli Perelman wrote: > >> Reviving this thread, doing any type of simple statistics is more verbose >> than it probably needs to be as calculating sums, averages, etc. makes most >> resort to Array reduction. I understand the need for methods such as >> `Math.hypot(...values)`, but for ECMAScript to evolve to be useful in >> statistics programming without needing to resort to R would definitely also >> be a nice-to-have. I'm sure there are many statistical functions you >> *could* add, but at a bare minimum, it would be useful: >> >> Math.sum(...values) or Math.summation(...values) >> Math.mean(...values) // Spelling out mean here is succinct while still >> intentional, instead of .avg >> Math.variance(...values) >> Math.stddev(...values) >> >> Obviously some of these can be computed from the results of others, but I >> think the convenience and intent of the methods, not to mention their wide >> usefulness outweigh the concern of Math bloat. Thoughts? >> >> P.S. Definitely not against even more core stats methods, but have to >> start somewhere. :) >> >> Eli Perelman >> Mozilla >> > > > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Additional Math functions
On 10/01/2015 23:10, Sebastian Zartner wrote: While Math.sum() and Math.mean() currently don't exist, they can easily be polyfilled: See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Sum_all_the_values_of_an_array for summarizing the values of an array and the following code for building the average of the array values: let arr = [0, 1, 2, 3]; let total = arr.reduce(function(a, b) { return a + b; }); let mean = total / arr.length; Calculating the variance and standard deviation would require a bit more code, though are also easy to polyfill. Non-the-less I can see the need for having standard functions for this. Sebastian Yes, Math.sum can be polyfilled, but doing so if you want accurate answers takes a fair amount of care. The code above is pretty bad as a polyfill because it sometimes produces highly inaccurate answers. For example, if arr = [1, 1e18, -1e18], then this polyfill will return the incorrect value 0 for total, while a more careful implementation would return 1. Waldemar ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
rest parameters
Hi all, I'm coming to you for a tiny question... excuse me if already replied... Where the rest parameter are only declarable at the end of the arguments list, like this, please? ` void function (a, ...b, c) { // b = [2, 3] }(1, 2, 3, 4); ` Any real reasons? Michaël Rouges - https://github.com/Lcfvs - @Lcfvs ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: rest parameters
I guess this is another reason why promisified versions are better: ```js somethingAsync(foo, bar, ...args).then(yourCallback) ``` On 2 October 2015 at 20:31, Michaël Rougeswrote: > For me, the rest parameters must be unique per arguments list but, > logically, usable following the function requirements... > > A classical example, the node.js methods, which takes a callback as last > argument, preceeded (generally) by one or more arguments, for a same method. > > Michaël Rouges - https://github.com/Lcfvs - @Lcfvs > > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Additional Math functions
I really don't think I'd want a basic `Math.sum(a, b, c)` meaning anything other than `a + b + c`, i.e. `(a + b) + c`. We should all just come to terms with the fact that floating point addition is not associative. Or is there really some simple, O(n) algorithm to do a better (more "careful") job? Cheers On 2 October 2015 at 21:23, Waldemar Horwatwrote: > On 10/01/2015 23:10, Sebastian Zartner wrote: > >> While Math.sum() and Math.mean() currently don't exist, they can easily >> be polyfilled: >> See >> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Sum_all_the_values_of_an_array >> for summarizing the values of an array and the following code for building >> the average of the array values: >> >> let arr = [0, 1, 2, 3]; >> let total = arr.reduce(function(a, b) { >>return a + b; >> }); >> let mean = total / arr.length; >> >> Calculating the variance and standard deviation would require a bit more >> code, though are also easy to polyfill. >> Non-the-less I can see the need for having standard functions for this. >> >> Sebastian >> > > Yes, Math.sum can be polyfilled, but doing so if you want accurate answers > takes a fair amount of care. The code above is pretty bad as a polyfill > because it sometimes produces highly inaccurate answers. For example, if > arr = [1, 1e18, -1e18], then this polyfill will return the incorrect value > 0 for total, while a more careful implementation would return 1. > > Waldemar > > > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: rest parameters
On Fri, Oct 2, 2015 at 12:09 PM, Steve Finkwrote: > I don't know, but I can speculate. It's not at all obvious how ...args in > the middle should behave: what if you have two rest arguments? Is that > forbidden, or is one greedy? What if one of the trailing parameters has a > default value? Also, iiuc the spec treats "undefined" the same as > "nonexistent" in most places. So what should your function do when passed > (1, 2, 3, undefined)? > > In short, it seems like a hairball of complexity for no real gain. Yes, this has been discussed in the past, and issues like what you brought up are the reason we've rejected it. There's lots of ambiguous situations, no clear answer for most (any?) of them, and the benefit is minimal. ~TJ ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: rest parameters
For me, the rest parameters must be unique per arguments list but, logically, usable following the function requirements... A classical example, the node.js methods, which takes a callback as last argument, preceeded (generally) by one or more arguments, for a same method. Michaël Rouges - https://github.com/Lcfvs - @Lcfvs ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Additional Math functions
In addition, using reduce just to sum array elements forces a function execution for every element. May not be bad for a minimal count of elements, but doing statistical work on larger collections would benefit from having an optimized summation. Eli Perelman On Fri, Oct 2, 2015 at 3:23 PM, Waldemar Horwatwrote: > On 10/01/2015 23:10, Sebastian Zartner wrote: > >> While Math.sum() and Math.mean() currently don't exist, they can easily >> be polyfilled: >> See >> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#Sum_all_the_values_of_an_array >> for summarizing the values of an array and the following code for building >> the average of the array values: >> >> let arr = [0, 1, 2, 3]; >> let total = arr.reduce(function(a, b) { >>return a + b; >> }); >> let mean = total / arr.length; >> >> Calculating the variance and standard deviation would require a bit more >> code, though are also easy to polyfill. >> Non-the-less I can see the need for having standard functions for this. >> >> Sebastian >> > > Yes, Math.sum can be polyfilled, but doing so if you want accurate answers > takes a fair amount of care. The code above is pretty bad as a polyfill > because it sometimes produces highly inaccurate answers. For example, if > arr = [1, 1e18, -1e18], then this polyfill will return the incorrect value > 0 for total, while a more careful implementation would return 1. > > Waldemar > > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: rest parameters
On 10/02/2015 11:52 AM, Michaël Rouges wrote: Hi all, I'm coming to you for a tiny question... excuse me if already replied... Where the rest parameter are only declarable at the end of the arguments list, like this, please? ` void function (a, ...b, c) { // b = [2, 3] }(1, 2, 3, 4); ` Any real reasons? I don't know, but I can speculate. It's not at all obvious how ...args in the middle should behave: what if you have two rest arguments? Is that forbidden, or is one greedy? What if one of the trailing parameters has a default value? Also, iiuc the spec treats "undefined" the same as "nonexistent" in most places. So what should your function do when passed (1, 2, 3, undefined)? In short, it seems like a hairball of complexity for no real gain. ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Additional Math functions
On 10/02/2015 13:30, Alexander Jones wrote: I really don't think I'd want a basic `Math.sum(a, b, c)` meaning anything other than `a + b + c`, i.e. `(a + b) + c`. We should all just come to terms with the fact that floating point addition is not associative. Or is there really some simple, O(n) algorithm to do a better (more "careful") job? Kahan summation is simple and O(n). There exist efficient algorithms to get the exact sum as well. See, for example, http://www.ti3.tuhh.de/paper/rump/RuOgOi07I.pdf Waldemar ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: rest parameters
For the undefined case, it isn't unexistent... ```JavaScript void function () { console.log(arguments.length); // 3 }(1, 2, undefined); ``` Then, I don't see how it may be ambiguous... Michaël Rouges - https://github.com/Lcfvs - @Lcfvs ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Re: Additional Math functions
Interesting. I still feel that these algorithms should be given their proper names in a math library, because I would feel quite troubled if `Math.sum(a, b, c) !== a + b + c`. Maybe I'm alone in this view, though. What do other languages do? On Friday, 2 October 2015, Waldemar Horwatwrote: > On 10/02/2015 13:30, Alexander Jones wrote: > >> I really don't think I'd want a basic `Math.sum(a, b, c)` meaning >> anything other than `a + b + c`, i.e. `(a + b) + c`. We should all just >> come to terms with the fact that floating point addition is not associative. >> >> Or is there really some simple, O(n) algorithm to do a better (more >> "careful") job? >> > > Kahan summation is simple and O(n). > > There exist efficient algorithms to get the exact sum as well. See, for > example, http://www.ti3.tuhh.de/paper/rump/RuOgOi07I.pdf > > Waldemar > > > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: rest parameters
On Fri, Oct 2, 2015 at 3:00 PM, Michaël Rougeswrote: > For the undefined case, it isn't unexistent... > > ```JavaScript > void function () { > console.log(arguments.length); // 3 > }(1, 2, undefined); > ``` > > Then, I don't see how it may be ambiguous... Again, this has been discussed in the past. It would be useful for you to look it up in the archives and find the previous discussion. A few examples of the ambiguity: function f1(...a, b, c) {print arguments} f1(1) // ([], 1, undefined) or ([], undefined, 1) ? function f2(...a, b="default") {print arguments} f2(1) // ([], 1) or ([1], "default") ? function f3(a, b, ...c, d, e) {print arguments) f3(1,2,3) // (1, 2, [], 3, undefined) or (1, 2, [], undefined, 3) or (1, undefined, [], 2, 3) or something else? With the rest param limited to being at the end, it's clear how to answer questions similar to these - you just assign arguments normally, and then if there are any left over, they get packed up in the rest param. When arguments can follow the rest param, it's not clear how to do it. You can't just ignore the rest param, assign args, and then take the leftovers, because the rest param needs to take from the middle. You're clearly grabbing from "each end" and then if there's any left in the middle, you pack them into the rest param, but how to do so? That's the essence of the f1 and f3 ambiguities. And optional arguments (f2) are similarly difficult - should they "grab" more strongly than the rest param? You can come up with answers to these questions. What you can't do is come up with answers that are *obviously correct*. This is why I'm not aware of any language that allows this syntax. ~TJ ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Additional Math functions
On 10/02/2015 16:37, Alexander Jones wrote: Interesting. I still feel that these algorithms should be given their proper names in a math library, because I would feel quite troubled if `Math.sum(a, b, c) !== a + b + c`. Maybe I'm alone in this view, though. What do other languages do? On Friday, 2 October 2015, Waldemar Horwat> wrote: On 10/02/2015 13:30, Alexander Jones wrote: I really don't think I'd want a basic `Math.sum(a, b, c)` meaning anything other than `a + b + c`, i.e. `(a + b) + c`. We should all just come to terms with the fact that floating point addition is not associative. Or is there really some simple, O(n) algorithm to do a better (more "careful") job? Kahan summation is simple and O(n). There exist efficient algorithms to get the exact sum as well. See, for example, http://www.ti3.tuhh.de/paper/rump/RuOgOi07I.pdf Waldemar In the cases where the algorithms produce something other than the best practical results, giving them descriptive names would be useful. However, compound naming can then get out of hand if you also include functions such as average, standard deviation, etc., which include computing the sum as a subroutine. Waldemar ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss