10 ms·
This approach is perfect for the is-even npm package[1], with 196,023 weekly downloads, or the is-odd npm package[2], with 285,501 weekly downloads. Wouldn't it
by oefrha 3y ago
This approach is perfect for the is-even npm package[1], with 196,023 weekly downloads, or the is-odd npm package[2], with 285,501 weekly downloads. Wouldn't it be cool if you type `npm install` and it starts downloading a 40GB is-even and a 40GB is-odd?
[1] https://www.npmjs.com/package/is-even https://www.npmjs.com/package/is-even
[2] https://www.npmjs.com/package/is-odd https://www.npmjs.com/package/is-odd
- chrisandchris 3y agoThe funniest (or best part) about those two packages is that is-even has a dependency to is-odd and does just negate the output of is-odd.
- jl6 3y agoIt’s true, the great thing about clean, reusable, modular code like this is that you can compose both of these packages to make a is-even-or-odd package.
- lvncelot 3y agoWell first you need to obviously build an OR package, part of your suite of logical operator packages, all depending on your battle-tested, high performance XOR package.
- wiseowise 3y agoGroundbreaking!
- kparaju 3y agoYou joke... but https://www.npmjs.com/package/is-odd-or-even https://www.npmjs.com/package/is-odd-or-even
- layer8 3y agoIf would be even funnier if is-odd would simultaneously do the same thing in reverse.
- chrismorgan 3y agoActually it’s not good enough there because JavaScript numbers are f64 rather than u32. Even if you only support the safe integer range (beyond which the answers become somewhat meaningless), that’s 2⁵⁴, more than four million times as large as 2³². Not sure how big the machine code would be, but I’m guessing it’ll just add 4 bytes (40%) to each case, so you’re up to something like 224 exibytes. And that’s when you’re being lazy and skipping the last ten bits’ worth. If you want to do it properly, multiply by another thousand. Or maybe a little less, I haven’t thought too deeply about NaN patterns. Or maybe just infinite if it supports bigints.
- deepsun 3y agoBut we can probably compress the data very well (e.g. "double delta" is always zero), and then decompress only needed parts.
- vineyardmike 3y agoThis still requires reading through it all in memory at run time. Maybe we can optimize it with a JIT -what if the package called the ChatGPT api to get the the python code that generates the machine code just for the number queried?
- Someone 3y agoBut you can create a self-modifying function that, ‘for speed’, adds each case when it is called. The initial function should start with checking for special cases NaN, infinities, 0 and -0, then do (may not be valid JavaScript) if((x++ === x) && (x—- === x)) printf("even\n"); to handle cases over 2^54 or below -2^54, then do isOdd = true neg = x pos = x while(true) { if(neg++ === 0) break; if(pos-- === 0) break; isOdd = !isOdd } to determine whether x is odd or even in isOdd. Doing the self-modification is left as an exercise. This is good defensive programming. It avoids doing a tricky division by 2.0 that might be buggy (I don’t think https://en.wikipedia.org/wiki/Pentium_FDIV_bug https://en.wikipedia.org/wiki/Pentium_FDIV_bug was affected, but who knows what other FPUs do?)
- lvncelot 3y agoIt's always worth mentioning that those packages stem from the effort of one dedicated npm spammer[1] to weasel his way into as many node_modules directories as possible. There's also packages for ansi-colors (no, not one package for all colors, one package for each color) and god knows what else. These then get jammed into any cli tool or other half-way sensible looking package, all referencing each other obviously - and any "real" project is just a single innocuous dependency away from having dozens of jonschlinkert packages. [1] https://www.npmjs.com/~jonschlinkert https://www.npmjs.com/~jonschlinkert
- jevoten 3y agoAchieving supply-chain security by controlling every link in the chain. This Jon is very noble.
- jbverschoor 3y agoPackages should specify their entitlements.. network access, file access, executing system calls, being able to monkey patch things, accessing packages outside their own.
- vorticalbox 3y agoDeno has a few of these by default an application has no access to the filesystem, env or network. You can allow only reading or only writing for files and you can also define what domains are allowed.
- jbverschoor 3y agoSure it's a huge step forward. But that's on an application level, which honestly I can do with sandbox-exec on macOS (see below). In Deno, all permissions would still propagate down to the dependencies. (version 1) (deny default) (allow file-read* (subpath "~/Downloads") ) (allow network-outbound (remote tcp "localhost:80") ) or (version 1) (allow default) (deny network*) or # start an airgapped shell, and play around in there.. sandbox-exec -p "(version 1)(allow default)(deny network*)" bash
- cyco130 3y agoBut of course WebAssembly is the future. So we should reimplement it in WASM.
- christophilus 3y agoEvery time I see these packages mentioned, I’m reminded of an interview with Joe Armstrong. In it, he said, (paraphrasing from memory) “Wouldn’t it be interesting if a language had a global, flat registry of functions that anyone could pull from and contribute to? That way, bugs are fixed in one place, there’s less reinvention of the wheel, things get more correct over time, and programs just become a simple composition of standard functions.” I may be misremembering his meaning, but I remember thinking it was an interesting idea. It wasn’t obviously a terrible idea. I thought it would be like the Clojure standard library on steroids, especially if it was coordinated and vetted by a decent team. But alas, NPM has proven it otherwise.
- harperlee 3y agoI don't think NPM has proven that idea infeasible, just that it may not be a good idea to depend on third-party content that may change under your feet, on such a fine-grained level. But have a look at the Unison language https://www.unison-lang.org/docs/the-big-idea/ https://www.unison-lang.org/docs/the-big-idea/ , that has such global registry but addresses each function by a hash of its syntax tree, and thus sidesteps the issue.
- ncruces 3y agoHow are bugs "fixed in one place" that way? You either accept updates of third party packages, with little to no vetting of your own, and get fixes "for free" or… you don't. There's no middle ground: the only way to save effort is to trust others.
- MisterTea 3y agoNPM is a worse idea than Joe's as Joe was talking about a single community vetted monorepo. Not a free for all of individual repos. With Joe's idea everything is up front and part of the language and not a bulletin board of packages near the checkout line at the git supermarket. This way simple stuff like isint() can make its way into the languages official standard library. This should eliminate the uncertainty of 3rd party packages maintained by a random number of individuals that can be taken down or tainted at any time.
- mickael-kerjean 3y agoI did a nullll package [1] whose only purpose is to export a null and takes 400MB in memory but somehow it got flagged in HN [2]. With 41 stars on github and 100% test coverage [3], it was clearly production ready. [1]: https://github.com/mickael-kerjean/nulll https://github.com/mickael-kerjean/nulll [2]: https://news.ycombinator.com/item?id=17072675 https://news.ycombinator.com/item?id=17072675 [3]: https://github.com/mickael-kerjean/nulll/blob/master/test.js https://github.com/mickael-kerjean/nulll/blob/master/test.js
- lvncelot 3y agoThat try-catch block[1] is a thing of beauty. [1] https://github.com/mickael-kerjean/nulll/blob/master/index.js#L2 https://github.com/mickael-kerjean/nulll/blob/master/index.j...
- tiagod 3y agoShame it was flagged. Would've been handy for a project I was working on. Had to work around not having a performant null
- deleted 3y ago[deleted]
- divbzero 3y agoAmazingly, following “don’t repeat yourself” in its purest form, is-even depends on is-odd: /*! * is-even <https://github.com/jonschlinkert/is-even> * * Copyright (c) 2015, 2017, Jon Schlinkert. * Released under the MIT License. */ 'use strict'; var isOdd = require('is-odd'); module.exports = function isEven(i) { return !isOdd(i); };
- goalieca 3y agoIf this code runs on hundreds of millions of phones and computers, npm seems horrible for the environment! Not only because of runtime, but because we’re talking so much just to host and serve these modules!
- fritzo 3y agoThese packages are performance art, and shall not be judged by efficiency metrics.
- asylteltine 3y agoThat’s right. Npm and the entire node ecosystem is just terrible. Specifically because you can get a production ready package from even Google, and you know it’s going to have some random useless package in the chain somewhere.
- jrpelkonen 3y agoImplementing the comparisons by hand seems difficult and primitive. May I suggest introducing some helpful sub-packages and building the solution on top of those. For instance, is-odd would be implemented by using is-one, is-three, is-five, is-seven, etc.
- leeoniya 3y agoand is-one should be implemented by negating is-two, is-three, and so forth
- paddy_m 3y agoI read some text where a cheme was implemented such that numbers were `2 = 1+1`, `3 = 2+ 1`...
- jcparkyn 3y agoProbably Peano numbers. https://wiki.haskell.org/Peano_numbers https://wiki.haskell.org/Peano_numbers
- jbboehr 3y agoPerhaps Nock: https://developers.urbit.org/reference/nock/definition https://developers.urbit.org/reference/nock/definition > The reader might wonder how an interpreter whose only arithmetic operation is increment can ever be practical. > The short answer is that a Nock interpreter doesn't have to use the algorithm above. It just has to get the same result as the algorithm above.
- avinassh 3y agoI did something similar six years ago called gg-flip for npm - https://github.com/avinassh/gg-flip https://github.com/avinassh/gg-flip It was submitted in HN too - https://news.ycombinator.com/item?id=16194932 https://news.ycombinator.com/item?id=16194932
- jgalt212 3y agoI'd fire someone who imported those packages.
- atleta 3y agoI haven't heard about him but checking the source tree of our 2 front-end apps and his 'is-number' package (which his is-odd depends on), seems to be imported by quite a few other packages. Now looking at the source, that package may make sense if figuring out whether something is a number type in JS is really that cumbersome. (Though I'd expect that there is a more generic package that covers the other built-in types as well.) Also since isNumber treats strings that can be converted to a number, a number, it can yield weird results since adding two strings will naturally just concatenate them. So e.g.: const a = '1'; isNumber(a); // returns true const b = a + a; // Now you have a string in b: '11' Of course, it's standard JS stupidity (and 2*a would be 2, and 1+'1' and '1'+1 would both be '11'), but then maybe stating that '1' is a number is not the right response. However, the package was downloaded 46 million times last week and that seems to be so low only because of Christmas. The previous weeks averaged around 70M. And most of these are dependencies, like in our projects, I'm sure.
- geodel 3y agoBig fan of code reuse. Small packages providing single unique functionality is the way to program modern systems. It has to be right if bright minds in NPM / Rust community follow this approach.
- ape4 3y agoI was wondering what the code looked like... 'use strict'; const isNumber = require('is-number'); module.exports = function isOdd(value) { const n = Math.abs(value); if (!isNumber(n)) { throw new TypeError('expected a number'); } if (!Number.isInteger(n)) { throw new Error('expected an integer'); } if (!Number.isSafeInteger(n)) { throw new Error('value exceeds maximum safe integer'); } return (n % 2) === 1; };
- cchance 3y agoI mean... when you look at it like that, at least it's got the error checking integrated as well... tho the fact it pulls in is-number is fucking hysterical lol
- kparaju 3y agoeven (https://www.npmjs.com/package/even?activeTab=dependencies https://www.npmjs.com/package/even?activeTab=dependencies) pulls is-even. is-even (https://www.npmjs.com/package/is-even?activeTab=dependencies https://www.npmjs.com/package/is-even?activeTab=dependencies) pulls is-odd. is-odd (https://www.npmjs.com/package/is-odd?activeTab=dependencies https://www.npmjs.com/package/is-odd?activeTab=dependencies) pulls is-number. sigh
- jkrubin 3y agoTheoretically only one 40gb file would be needed. AFAICT almost every odd number is not even (I cannot say "all" bc I have not checked every integer yet).
- blowski 3y agoThat's what unit tests are for, right?
- dfee 3y agoThe implementation of is-even is mind blowing (numbing?). 'use strict'; var isOdd = require('is-odd'); module.exports = function isEven(i) { return !isOdd(i); }; good thing it declares its dependencies properly!
- xorvoid 3y agoEven better if it built-on-install by executing another script to generate everything (naturally using %) New nerd game: enshitification inverse golf.
- akoboldfrying 3y agoInteresting that there are so many more weekly downloads for is-odd. This seems to suggest that there are many more odd numbers than even. (Alternatively, perhaps odd numbers are actually much rarer, leading to them having a higher market value, and thus more interest in discovering them.)
- Benjaminsen 3y agoThe isEven package uses the isOdd package…
- xstas1 3y agoI wonder what will happen if the author of isOdd optimizes their code by using the isEven package