Numeric Array prototypes (Isiah Meadows)
# Isiah Meadows (6 years ago)
This reply from Gbadebo missed the list. (I confirmed in a private email.)
Isiah Meadows contact at isiahmeadows.com, www.isiahmeadows.com
This reply from Gbadebo missed the list. (I confirmed in a private email.) ----- Isiah Meadows contact at isiahmeadows.com www.isiahmeadows.com On Wed, Nov 21, 2018 at 7:16 PM Gbadebo Bello <gbahdeybohbello at gmail.com> wrote: > > Yh, i felt the same way about the rest also but thought it would be nice to atleast get feedback before concluding. > > On Wed, Nov 21, 2018, 06:08 Isiah Meadows <isiahmeadows at gmail.com> wrote: >> >> Not sure these belong in the standard library itself. I could see max and min being there (these are very broadly useful), but the others, not so much. I'd also like both max and min accept an optional comparison callback of `(a, b) => a < b`. But the rest seem too narrowly useful IMHO. >> On Tue, Nov 20, 2018 at 21:38 Gbadebo Bello <gbahdeybohbello at gmail.com> wrote: >>> >>> The array object has no method for finding the minimum value of an array of numbers and the Math.min() method doesn't work on Arrays right out of the box. I'll have similar issues if i wanted to return the maximum number, the mean, mode, median or standard deviations in any array of numbers. >>> >>> This is a bit tasky and can be done in just a line of code if there where Array.prototype.{method}() for it. >>> >>> My proposal is, Arrays should have the following prototypes >>> >>> Array.prorotype.min() >>> Array.prorotype.max() >>> Array.prorotype.mean() >>> Array.prorotype.median() >>> Array.prorotype.mode() >>> Array.prototype.stdev() >>> >>> The mean, median and mode would be very useful when visuali >>> Here's is a polyfill of what i'll love to propose. >>> >>> //Array.prototype.min() >>> Array.prototype.min = function(){ >>> let arrayVal = this; >>> let checkNonNumbers = this.some(element => { >>> if(typeof(element) !== typeof(Math.random())){ >>> throw new Error("This Array.prorotype.min() takes only array of numbers as arguments"); >>> } >>> else{ >>> return true; >>> } >>> }); >>> function findMin(validated){ >>> if(validated == true){ >>> return Math.min.apply( Math, arrayVal ); >>> } >>> } >>> return findMin(checkNonNumbers); >>> } >>> >>> Similarly, Array.prototype.max() can be implemented as above. >>> >>> >>> >>> //Array.prototype.median() >>> Array.prototype.median = function(){ >>> let values = this; >>> function median(values){ >>> values.sort(function(a,b){ >>> return a-b; >>> }); >>> if(values.length ===0) return 0 >>> var half = Math.floor(values.length / 2); >>> if (values.length % 2) >>> return values[half]; >>> else >>> return (values[half - 1] + values[half]) / 2.0; >>> } >>> return median(numbers) >>> } >>> _______________________________________________ >>> es-discuss mailing list >>> es-discuss at mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss
Yh, i felt the same way about the rest also but thought it would be nice to atleast get feedback b concluding.
---------- Forwarded message --------- From: Gbadebo Bello <gbahdeybohbello at gmail.com>
Date: Mon, Nov 19, 2018, 01:27 Subject: Re: Numeric Array prototypes (Isiah Meadows) To: Isiah Meadows <isiahmeadows at gmail.com>
Yh, i felt the same way about the rest also but thought it would be nice to atleast get feedback before concluding.