9 ms·
Similarly, the `average` package on NPM is one that I came across: https://www.npmjs.com/package/average https://www.npmjs.com/package/average var average
by jperras 11y ago
Similarly, the `average` package on NPM is one that I came across:
https://www.npmjs.com/package/average https://www.npmjs.com/package/average
var average = require('average');
var result = average([2, 5, 0, 1, 25, 7, 3, 0, 0, 10]);
console.log('The average for all the values is:', result);
It's hard to not stare at that in complete disbelief; someone thought that it was worthwhile to create a package for determining the mean of an array of numbers.
- nv-vn 11y agoAnd 54 people found it a worthwhile package to install _this week alone_.
- agumonkey 11y agoLet's say these are just prescient trolls. For our sanity.
- jessaustin 11y agoThat could be one dependent module that got installed in 54 places. More likely, it was mostly various bots that install everything on some semi-regular schedule.
- rtpg 11y agoWell a bunch of standard libraries have sum defined, right? JavaScript has suffered from a lack of a standard library for a while. Having a small package like this means that (in theory) everyone is using the same bug free version of summing instead of writing their own. Honestly having JS start building a standard library at this point would be wonderful.
- guscost 11y agoThere's already `Array.prototype.reduce()` in most browsers, which will do summing, string concatenation, and any other kind of aggregation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... If you need something more convenient I highly recommend just adding lodash and getting a whole bag of rock-solid awesome tools.
- astrange 11y agoreduce isn't a safe way to add floating-point numbers unless your accumulator is much more precise than your array values. You'll end up with what they call "catastrophic cancellation". And they won't let you have fixed-point in JavaScript!
- guscost 11y agoDoesn't a for loop have the same problem?
- astrange 11y agoIf you write the loop yourself, you can add everything to a double instead of a float, or a long double instead of a double, if you have those. Oh, if all your numbers are positive you can also sort the array and it will minimize the error accumulated, but I'm not sure what to do with mixed signs.
- sotojuan 11y agoFor now I suggest using lodash functions exported as modules. For example: https://www.npmjs.com/package/lodash.mean https://www.npmjs.com/package/lodash.mean Or, in light of `left-pad`: https://www.npmjs.com/package/lodash.padstart https://www.npmjs.com/package/lodash.padstart You get the benefit of a "small, focused" module but still rely on a large and stable project with a great maintainer that cares about performance and testability.
- metaphorm 11y agohere is the source for that npm package https://github.com/bytespider/average/blob/master/src/average.js https://github.com/bytespider/average/blob/master/src/averag... and here is the code copied out in full module.exports = function average(values) { for (var i = 0, sum = 0; i < values.length; ++i) { sum += values[i]; } return sum / values.length; }; is this worth adding a dependency to your build for?
- michaelbuckbee 11y agoI'm thinking that someone wanted to learn about building and publishing a package and the ecosystem so they made this computationally trivial thing as a practical exercise. Pretty much every package management system gets cruft in it like this. Example: for a long time someone had uploaded a random Wordpress core install into Bower.
- lsiebert 11y agoYou know what's worse? Javascript numbers are all floating point numbers, which means integers are 53 bits long. So, you might think this library would try to address issues this can cause, but nope, this is the average statement you'd write if you didn't know was a mantissa was and had never heard of big.js, bignumber.js, decimal.js, crunch.js or even strint (which represents integers as strings because wtf not).
- sdegutis 11y agoEvery once in a while, I come to HN, and inevitably find a post about some new JavaScript technology on the front page, wherein I find some comment that gives me a new appreciation for the web app language I use at work, which fortunately is not JavaScript.
- artine 11y agoI'd be interested to know what language it is.
- bbcbasic 11y agoIt must be one of: Elm, Purescript, GHSJS, Faye or Haste
- monitron 11y agoWhat is the web app language you use at work? I assume you're talking about something that runs on the client -- a web browser -- so it probably compiles down to JavaScript, right? JS literally has no numeric type that isn't a float, so how does your language escape this fundamental limitation?
- Jach 11y agoNot OP, but I used Google's Long.js (among other things) in a project[0] to get around the limitation of not being able to do as in Python: import fractions print str(fractions.Fraction.from_float(0.3333).limit_denominator(10)) Almost anything that compiles to JS would be better than by-hand JS; I'd love to use ClojureScript with re-frame at work... [0] http://www.thejach.com/pseudo-public/webgl_coursera/assignments/week2/approx_fraction.js http://www.thejach.com/pseudo-public/webgl_coursera/assignme...
- superuser2 11y agoThis sounds like a symptom of an inadequate standard library. I do expect to be able to call "average" on a list of numbers without writing it myself, but I expect that to be part of the language not a 3rd party package.
- joshmanders 11y agoIt should, but the ECMAScript spec doesn't have it, and Node.js team prefers to keep the core libraries to a minimum and definitely do not want to modify prototypes. Thus we depend on userland to provide those features for us.
- jay_kyburz 11y agoThats why somebody should fork Node and start building a batteries included standard library.
- ansgri 11y agoIt seems C++ approach is quite good: you have a very stable (if a bit too stale) standard library, and you have Boost, which is a cutting-edge batteries-included extension to stdlib curated by much the same people as in the standardization committee. It serves as a great _inclusive_ counterpart to the _exclusive_ stdlib and a sandbox for stdlib extension proposals.
- sago 11y ago> I do expect to be able to call "average" on a list of numbers without writing it myself Just out of interest, what kinds of functions would you expect to have to write yourself, if you're not happy about calculating the average of a list of numbers?
- superuser2 11y agoThose that are unique to the problem I'm actually trying to solve, and not already in the standard libraries or well-maintained packages of mainstream languages.
- elwell 11y agoSounds like the author was aiming for something to put on his resume: e.g., "Author of 25 libraries on NPM, some with more than 500K downloads." etc...
- anexprogrammer 11y agoWell if I was interviewing the guy, my second or third question would be "tell me about these packages you published on NPM" It's going to be damn hard to make an average function sound impressive.
- formula1 11y agoIm of the mind most employers are looking for "have you done stuff" "can you do stuff". Veey rarely are they looking for anyone extraordinary, simply a tool that works
- fieryeagle 11y agoI doubt something like 'wrote npm package that averages stuff' would be relevant without fluffing it up to impress the non-tech hiring manager. Still would ring alarm bells with devs though.
- Someone 11y agoIn JavaScript, the API could be fairly complex. Thing is, JavaScript doesn't have arrays of numbers. So, what should the function do if the array passed in contains a null, an undefined, a string such as "7", "012", "008", or "2+3", a function, another array, a hash, etc? I can easily see this grow into a function accepting options indicating such things as "null values are zeroes", "ignore null values", "evaluate string arguments", etc, or maybe into a factory that produces customized averaging functions. For example, average([null,1,2,3],[4,5]) could either be 3 (the average of all numbers), 2.5 (sum of numbers divided by number of 'root' items), undefined (input format error), 3.25 (average of average of [1,2,3] and average of [4,5]), etc. And what if it gets passed arrays of equal size? "average([1,2,3],[7,4,5])" could easily be interpreted as a vector average, and produce [4,3,4] Silly? Yes, but if you want to write a robust library in JavaScript, you have to think about what to do with all these kinds of input. And of course, there are the simple variants of harmonic mean, arithmetic mean, and geometric mean, and several others (https://en.m.wikipedia.org/wiki/Average https://en.m.wikipedia.org/wiki/Average)
- elwell 11y agoI think the scarier thing there is this line: "0.0.1 is the latest of 2 releases" I wonder if there will be a 3rd release with an updated averaging function. Edit: to be fair, it was an optimization from reduce to for loop [https://github.com/bytespider/average/commit/7d1e2baa8b8304d6918119874327854a70a61527 https://github.com/bytespider/average/commit/7d1e2baa8b8304d...]
- sklivvz1971 11y agoThat package is, at best, average... ducks (Edit: typo)
- abulman 11y agoDon't be .... Mean.
- danso 11y agoWhile it demonstrates the problem of npm lacking namespaces (such that the word "average" is wasted on such a trivial implementation)...it doesn't seem anyone was using that library
- secoif 11y agonpm has namespaces. https://docs.npmjs.com/misc/scope https://docs.npmjs.com/misc/scope
- masterleep 11y agoYes, but it's MIT licensed. We also need average_GPL to go with it.
- userbinator 11y agoYou bring up a good point with respect to copyright/licensing --- at what point does code stop being copyrightable and become public domain simply due to pure triviality? There are not very many sane ways to write an averaging function, and the most straightforward one (add up all the value, then divide) would probably be present in tons of other codebases.