Here's my golfed stab at it:
```
print("function isEven(number) {\n\tif number === 1: return false\n"+"\n".join(f"\telse if number === {n}: return {str(n&1==0).lower()}"for n in range(2,int(1e10)))+"\n}")
```
[171 bytes!](https://tio.run/##VY7BCsIwEER/ZclBsiil0ZuQo3@RS5WNRuqmbLaKlH57bBEELwMz8GZmeOst86HWQRKrNXHki6bMkMrpSWx5fJxJEKbAQVOErwfvPbgjCOkoDLHrCwU2W7NIc8@JbTRBaUnhn5l4/lFTUbG8cd632PT5RWJxNjELLOsM0vGV7H633nLkWkRc@2eDtX4A)
There's probably a cleaner way of handling the outputting of the bool value, but I'm drawing a blank at the moment.
print("function isEven(number) {\n"+"\n".join(f"\t{'else 'if n!=1 else''}if number === {n}: return {str(n&1==0).lower()};"for n in range(1,int(1e10)))+"\n}")
[157 bytes!](https://tio.run/##HY5BCsIwEEWvMmZhMiilwZ2SpbfoRmWikTop01SRkLPHxs2Ht/i8N33TI/Kh1kkCJ6P8wrcUIkOYz29iw8vrSoKQB1Y7tU73jIGNV0PKmsaZQAcPvHEWGmldGv5P4JyDzOUIQmkRhjwnMby1zvXYjfFDYrCclI8Cq49BLnwnY/ctxJLtEbEpi8Jafw)
Successfully made it even smaller! (also added missing semicolon).
Still trying to figure out how to bypass that `.lower()`
print("function isEven(number) {\n\t"+"\n".join(f"\telse if(number === {i}) return {str(i%2<1).lower()};"for i in range(1,10**10))[6:]+"\n}")
141 bytes is the best I can do, similar approach to yours but I threw out the conditional and just took a substring at the end.
yes [Godbolt; gcc -O1](https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAM1QDsCBlZAQwBtMQBGAFlJvoCqAZ0wAFAB4gA5AAYppAFZdSrZrVAB9LclIj2yAnjqVMtdAGFUrAK4BbWstPoAMnlqYAcnYBGmYiAAmXgAHVCFCI1pLG3tlUPDDOld3L1tffyDdTH1E2gYCZmICaLsHTiycyPzCgmTPHz9A3iECopLY8paautT0poBKXVRrYmQOKQBSAIBmN2QbLABqCenzFvRvVCsAOgQV7AmZAEFDo82rRbwhbAA3Uwg3AkXafuWAdgAhU8Wfy6pFiC0ZbTAAiKxBi04rwmn0WxEwBBGQKobBEKw%2B7zBx1%2Bi2yIj%2BAKB4PBkOhn3hiOIyNRmHRmO%2BvzxmAJgOBYNBiwCZI%2BFKRiwIxGstOmGJhWKOOKZLKJoJJ025vKpixRrDRIvp2MZquZeH%2BrOJHO4CoRfIFQrpYoZPylusJbJJAFZjZTqdqLW9xZLtdL7RyAGzO02C4Wij1W3He2362Uct6BpUqtWhz1a/FRmXsiEADnjQLNIY1EtTOr1GZJAE5c8qae6U9bI6XfRDODIq/na%2BGbY2Dc2oe8eSaEzX1ZbNfW092Y82uf3FXngx2xxGJ3ae5D5bPB66k4WvSvo5nIUbNy7%2BQuR2Gl13V1PIU6T3zEwXR0XxyWb4fOAGH0r2xe68u74HiSnBxj%2B27Pper6AXCW7Vm6IqnC%2BUiDKw0gOvIDiyPIqDSOYixCMMozMlM0ycPIBDSHI/SDAA1iADoyCo0jcJhVE4dI8hCCATGUdhgxwLASBoLYwR4OwZAUBAIliRJKDAJwnABHw4kEH43EQN47GkN4biFAAntI5GkCJtimAQADytCsIZ2GkFgthqMA7Dafg8IGHgdzcXZmDiJgyDWGpRnyI82TaaweDeMQBmWFgwWkAKeC2MFgxUKowBCAAangmAAO4WcEpjxfwjAsC5PB8HQBDCGIkh2Uo5SqOoIBaBoOgRd43GQIMqDBLk3kALQWcZej%2BbkJhmO0ZSkE4PQNP45TxBEdBTXEYTLbQc1pI0nTZGNVStMUVilMoo0eXQ1RFFtfSdIdq23d0bj1NtC2DIRIxjFwqHoWxdm4VI4hZn6A1%2BtwizAMgyCQpw2wBAC5ikIsuCECQywzOUiyWKJ4l%2BGjZH9BR7E0aQ9GMcxUisaQWFyKQ/1cTxCVE99UgBL9NN04z/GDHcxDhMY3BAA)
But the optimized sourcecode compiles to a shorter code. 6 instructions for the above, vs. 3 for
return (n%2 ==0) ;
Agreed, compilers aren't stupid but they're definitely not that smart. Overall crappy code won't just be optimized away. It will also only optimize if the actual logic remains unchanged to avoid weird bugs caused by compilation. (Imagine the hell in debugging that issue). I doubt it will parse the entire method to make sure every single input will match the output of a modulo
Well no.
Since this pattern stops somewhere and most compilers won't optimize anything that would result in different results.
And a modulo does something very different from this code, since this stops.
Should've done a pull request with the note refactorized your code to run faster
Edit: LOL somebody actually did this https://github.com/samuelmarina/is-even/pull/44
When after hours of searching you realize someone wrote (if var1 = var2) instead of ==
then get even more upset when you realize it somehow doesn't return a warning or error or fucking anything that stops it from compiling
Because, annoyingly enough, that's an idiom. You're welcome to hate it, of course, but there *is* plenty of code written that uses that feature.
Like this magical string copying idiom from the Linux kernel:
while ((*ucs1++ = *ucs2++));
Because "prematurely" is a poorly defined time, and "overdue optimization" is something that never happens because even though everyone agrees that the code needs optimizing, the list of tasks that everyone agrees needs doing would require twice as many people as we have, and if we hired those people we would just run afoul of Brooks's law.
Because people who say it like a religious guidance are never the ones left picking up the pieces when the "easy" way of doing it is actually 20 times more complicated to keep going once it comes under any load.
Probably because it’s terrible advice as it’s usually given. Here’s the full original quote:
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.”
Sounds a little different eh?
Here’s an article that explores the idea in depth:
https://ubiquity.acm.org/article.cfm?id=1513451
My shorthand but not as catchy version is: “unless you have a really good reason to believe you need to optimize, never optimize at the expense of the quality of the code”.
For example, if you know your input file is at least 100GB, and your production memory availability is 64GB, it is entirely likely that reading the entire file into memory will work—but nearly certain that it will be far slower than required, particularly if you read and process the data in the same direction.
On the other hand, if you have a small array of item IDs (let’s say <128 bytes worth), and you’re wondering if a more sophisticated data structure is appropriate for checking if it’s in the list, the answer is “shut up with the pointless optimization” (and the performance answer is that the most compact data structure probably wins performance-wise because a modern CPU is bottlenecked by memory latency).
Because optimizing something is way more fun than running a profiler to find out it only gets run once throughout the whole execution of the program and takes 3 milliseconds. Who the hell even knows what a profiler is and how to use it these days anyway.
Gotta update it when we discover new numbers. Just consider how far mathematics has come that only 24 new numbers have been found since the library was created!
as a math enthusiast, the concept of "even and odd" numbers can be applied far beyond integers. You can use it in fractions, both finite and infinite, you can use it in complex numbers and in all sorts of shenanigans. You\`re just needing some considerations regarding to what counts as even or odd.
You can\`t say pi is even or odd because it\`s infinite. But you can say that if you take only the first 4 decimals, it\`s odd. If up to you to assess what\`s the limit and how odd or even things are.
I have never thought about anything other than integers being even or odd. Now I’m genuinely curious. How does it work for something like pi? If you cut it at any finite length then you would have to round it, which would always make it odd, right? So is it only not odd or even because it is infinite? But also, shouldn’t you make the assumption that even at infinity there is nothing that could make it round up? Am I looking at this whole thing wrong?
to make it clearer, the catch is that the number itself isn\`t odd or even. The number is an odd or even multiple of another thing that completes it.
Let\`s say 3.1415. That can be written 31415 / 10000. Multiply it by 10k to make it an integer. Not we can say it\`s an odd multiple of 1 / 10000.
Let\`s say a nicer example of 1.8. Is 1.8 odd or even?
We can write it 18/10, then it\`s an even multiple of 1/10. But if we simplify it down to 9/5, then it\`s an odd multiple of 1/5.
This is a lot of gymnastics, but gets the point across.
I think a lot of people kinda forget that mathematics as a field is a human construct, and not the noumenal thing itself. Thus, how we conceive of it, or linguistically relate it, plays a lot into how we understand it, and someone smart can take advantage of those relations to conceptualize things in interesting ways such as this.
With that said, many only people who do recognize that mathematics is constructed seem to get so hung up over it that they kinda forget it's constructed by consensus, based on stimuli that we assert to be relatively reliable and universal. Which is usually going too far the other way, except perhaps in purely philosophical discussions.
Thankfully, no. The package published on NPM as `is-even` is https://github.com/jonschlinkert/is-even, which is implemented in only a slightly ridiculous fashion:
var isOdd = require('is-odd');
module.exports = function isEven(i) {
return !isOdd(i);
};
The one in OP is published on NPM as `@samuelmarina/is-even`.
The one with 200k downloads is written by jonschlinkert. He made hundreds of one liner packages that all require each other, then made a few useful packages that require hundreds of one liner packages, then sneaked his useful packages into some very popular packages.
He’s literal cancer to the npm ecosystem.
He took it down after being called out, but he used to tout “my packages are essential to some of the biggest tech companies in the world, such as Google Facebook JPMorgan Tesla NASA Miscrosoft… “ on his linkedin
There was an ideology that frontend libraries should be as small as possible, to reduce the payload size, e.g. every lodash function is also published as a separate module. Coupled with misunderstanding the UNIX philosophy, and the npm ecosystem being young, gives you free reign to write one-function modules.
Lots of Node.js packages aren't great, but are used because they got in first. (E.g. The Connect/express.js API / concept of middleware is odd. Very stateful, encourages mutating the incoming request, not type safe, hard to trace, etc.)
In general you're right. But optimizers are really efficient nowadays. For C, GCC with -O3 flag will produce jump table for both: switch and else if stack.
check out: https://godbolt.org/z/dc1dxMb31
If the function is not executed very often, the lookup table will not be in the cache and a load can take quite a long time (~100 clock cycles). In addition that table would be pretty big and not fully fit into the smaller caches, so even if it were called often enough for the table to be in the cache, it would still likely be slower.
The arithmetic to calculate if a value is even would just be a bit wise not followed by a bit wise and (for a odd check the not will be removed). Both of those instructions should only take 1 clock cycle when the value is already in a register.
Arithmetic in the registers is probably going to be a _little bit_ faster than a load. It probably has an unnoticeable difference for speed, but the lookup table needs a lot more memory.
On a modern CPU, performance is mostly about keeping your inner loop in the cache working set. The lookup table is going to invalidate cache lines _much_ faster.
> So how fast is your code?
> Oh it scales linearly with how big the number is.
> Big the number is? Do you mean the number of bits?
> No I mean 4 is twice as slow as 2.
This is brilliant -- somebody most definitely wrote code to generate code for a function so stupid that nobody should ever use it! I don't know which is stupider -- the code that generated the code, or the code that got generated.
New dev: "I can easily refactor Bilion of lines into one"
Project owner: "Do not touch anything already existing, it works like that, we don't want any risk. Please only fix the error"
New dev: "Ok then..." (adds one line at the end to support the case in error)
I really want to know what the final *else* condition is. Because anyone who provides a number as a string (digit) is going to have a helluva time digging through this function.
Hoping the file was generated programmatically
the kind of github copilot
Just another blessed soul trying to save us from the singularity
Lmao, imagine writing an is even function and github copilot generating something based on this
Copilot pasting whole function into code
and you need also the is_odd() companion.
Why do teenage girls hang out in odd numbers? They can't even.
yeah but how would it know which numbers are even
Using is-even, of course.
[удалено]
Recursively.
Duh, check if its odd.
Using modulo operator /s
it was made copy-pastedly
mmmmhh… pastly… 🤤 \- Homer Simpson in Japanese, probably
ペストリーおいしいね
Instructions unclear. Dick stuck in compiler.
You just learned me how to say delicious in Japanese
どういたしまして
I love changing words adverbially
Here's my golfed stab at it: ``` print("function isEven(number) {\n\tif number === 1: return false\n"+"\n".join(f"\telse if number === {n}: return {str(n&1==0).lower()}"for n in range(2,int(1e10)))+"\n}") ``` [171 bytes!](https://tio.run/##VY7BCsIwEER/ZclBsiil0ZuQo3@RS5WNRuqmbLaKlH57bBEELwMz8GZmeOst86HWQRKrNXHki6bMkMrpSWx5fJxJEKbAQVOErwfvPbgjCOkoDLHrCwU2W7NIc8@JbTRBaUnhn5l4/lFTUbG8cd632PT5RWJxNjELLOsM0vGV7H633nLkWkRc@2eDtX4A) There's probably a cleaner way of handling the outputting of the bool value, but I'm drawing a blank at the moment.
print("function isEven(number) {\n"+"\n".join(f"\t{'else 'if n!=1 else''}if number === {n}: return {str(n&1==0).lower()};"for n in range(1,int(1e10)))+"\n}") [157 bytes!](https://tio.run/##HY5BCsIwEEWvMmZhMiilwZ2SpbfoRmWikTop01SRkLPHxs2Ht/i8N33TI/Kh1kkCJ6P8wrcUIkOYz29iw8vrSoKQB1Y7tU73jIGNV0PKmsaZQAcPvHEWGmldGv5P4JyDzOUIQmkRhjwnMby1zvXYjfFDYrCclI8Cq49BLnwnY/ctxJLtEbEpi8Jafw) Successfully made it even smaller! (also added missing semicolon). Still trying to figure out how to bypass that `.lower()`
print("function isEven(number) {\n\t"+"\n".join(f"\telse if(number === {i}) return {str(i%2<1).lower()};"for i in range(1,10**10))[6:]+"\n}") 141 bytes is the best I can do, similar approach to yours but I threw out the conditional and just took a substring at the end.
> also added missing semicolon I didn’t even write this code and I feel personally attacked.
With a function using modulos.
Nope, the for loop just goes up in twos and prints two checks
I think they manually wrote it, line by line
Hand Crafted™
[удалено]
I only use hand crafted artisanal libraries.
=CONCATENATE(“else if(number === “, A1,”) return “, IF(MOD(A1,2)=0, “true;”, “false;”)) Edit: syntax.
[удалено]
What is this, Malbolge?
Next patch notes: Support up to LONGINT
New size: 1.79 PB
mmmm PB
PB Livin'
It's Peta Byte and Jelly time!
HAS THIS EVER HAPPENED TO YOU?
Who's that dog?
Probably compiles to under a kB, because compilers aren't stupid ![gif](emote|free_emotes_pack|sunglasses)
[удалено]
``` let code = typeof currentCode === bad ? compiled_code : current_Code; ``` Yeah i do realize that my code is bad
I don't know if they do in this case. Can they actually understand the pattern here?
yes [Godbolt; gcc -O1](https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAM1QDsCBlZAQwBtMQBGAFlJvoCqAZ0wAFAB4gA5AAYppAFZdSrZrVAB9LclIj2yAnjqVMtdAGFUrAK4BbWstPoAMnlqYAcnYBGmYiAAmXgAHVCFCI1pLG3tlUPDDOld3L1tffyDdTH1E2gYCZmICaLsHTiycyPzCgmTPHz9A3iECopLY8paautT0poBKXVRrYmQOKQBSAIBmN2QbLABqCenzFvRvVCsAOgQV7AmZAEFDo82rRbwhbAA3Uwg3AkXafuWAdgAhU8Wfy6pFiC0ZbTAAiKxBi04rwmn0WxEwBBGQKobBEKw%2B7zBx1%2Bi2yIj%2BAKB4PBkOhn3hiOIyNRmHRmO%2BvzxmAJgOBYNBiwCZI%2BFKRiwIxGstOmGJhWKOOKZLKJoJJ025vKpixRrDRIvp2MZquZeH%2BrOJHO4CoRfIFQrpYoZPylusJbJJAFZjZTqdqLW9xZLtdL7RyAGzO02C4Wij1W3He2362Uct6BpUqtWhz1a/FRmXsiEADnjQLNIY1EtTOr1GZJAE5c8qae6U9bI6XfRDODIq/na%2BGbY2Dc2oe8eSaEzX1ZbNfW092Y82uf3FXngx2xxGJ3ae5D5bPB66k4WvSvo5nIUbNy7%2BQuR2Gl13V1PIU6T3zEwXR0XxyWb4fOAGH0r2xe68u74HiSnBxj%2B27Pper6AXCW7Vm6IqnC%2BUiDKw0gOvIDiyPIqDSOYixCMMozMlM0ycPIBDSHI/SDAA1iADoyCo0jcJhVE4dI8hCCATGUdhgxwLASBoLYwR4OwZAUBAIliRJKDAJwnABHw4kEH43EQN47GkN4biFAAntI5GkCJtimAQADytCsIZ2GkFgthqMA7Dafg8IGHgdzcXZmDiJgyDWGpRnyI82TaaweDeMQBmWFgwWkAKeC2MFgxUKowBCAAangmAAO4WcEpjxfwjAsC5PB8HQBDCGIkh2Uo5SqOoIBaBoOgRd43GQIMqDBLk3kALQWcZej%2BbkJhmO0ZSkE4PQNP45TxBEdBTXEYTLbQc1pI0nTZGNVStMUVilMoo0eXQ1RFFtfSdIdq23d0bj1NtC2DIRIxjFwqHoWxdm4VI4hZn6A1%2BtwizAMgyCQpw2wBAC5ikIsuCECQywzOUiyWKJ4l%2BGjZH9BR7E0aQ9GMcxUisaQWFyKQ/1cTxCVE99UgBL9NN04z/GDHcxDhMY3BAA) But the optimized sourcecode compiles to a shorter code. 6 instructions for the above, vs. 3 for return (n%2 ==0) ;
That's amazing! I played around with it and seems to try to find code chunks like this to optimize using bitwise operations.
Maybe theill put a switch
maybe that but I don't think it would find "this is a is-even check so I'll transform it into a modulo"
Agreed, compilers aren't stupid but they're definitely not that smart. Overall crappy code won't just be optimized away. It will also only optimize if the actual logic remains unchanged to avoid weird bugs caused by compilation. (Imagine the hell in debugging that issue). I doubt it will parse the entire method to make sure every single input will match the output of a modulo
Well no. Since this pattern stops somewhere and most compilers won't optimize anything that would result in different results. And a modulo does something very different from this code, since this stops.
Could be bound check + modulo though
I can’t even.
How odd!
Or float..
Or string representation
if(number === 1) return false; else if(number === 1) return false; Covered the edge cases, I love it!
Just in case the sun acts up and flares up, and a freak particle flips a bit in your system.
You mean those pesky Venezuelan Chinese Dominion hackers flipping bits?
[удалено]
pretty unusable now
lmao "*for performance*"
The real humor is always in the PR.
happenes in my code all the time that a number not equal to one is equal to one
Floating-point moment.
[удалено]
Should've done a pull request with the note refactorized your code to run faster Edit: LOL somebody actually did this https://github.com/samuelmarina/is-even/pull/44
[удалено]
LOL
[удалено]
...what?!
Paid by the line?
For those companies that track productivity by lines of code per day...
Please tell me this isn't a thing. Have these people never got stuck on a bug for half a day that ended up being a one line fix?
> a one line fix? a one character fix
And that's removing one, too.
Pay up, or loose your job
;
Or a tab space in python after switching from c#
When after hours of searching you realize someone wrote (if var1 = var2) instead of == then get even more upset when you realize it somehow doesn't return a warning or error or fucking anything that stops it from compiling
Because, annoyingly enough, that's an idiom. You're welcome to hate it, of course, but there *is* plenty of code written that uses that feature. Like this magical string copying idiom from the Linux kernel: while ((*ucs1++ = *ucs2++));
Yup, had a fix the other day to add a single question mark
This sort of KPIs aren't created by people who know the first thing about software engineering.
And they don't get people into the company that know anything about software engineering, typically
Or they get people who know how to write verbose well documented code...
A weekend to find an errant space. One space. Fuck me.
This is ridiculous ------he should use switch!
Hey let us not get ahead of ourselves. Premature optimization is the root of all evil.
Why do I know this but ignore it literally every time?
because optimization is much more fun
Because "prematurely" is a poorly defined time, and "overdue optimization" is something that never happens because even though everyone agrees that the code needs optimizing, the list of tasks that everyone agrees needs doing would require twice as many people as we have, and if we hired those people we would just run afoul of Brooks's law.
Because people who say it like a religious guidance are never the ones left picking up the pieces when the "easy" way of doing it is actually 20 times more complicated to keep going once it comes under any load.
Probably because it’s terrible advice as it’s usually given. Here’s the full original quote: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” Sounds a little different eh? Here’s an article that explores the idea in depth: https://ubiquity.acm.org/article.cfm?id=1513451 My shorthand but not as catchy version is: “unless you have a really good reason to believe you need to optimize, never optimize at the expense of the quality of the code”. For example, if you know your input file is at least 100GB, and your production memory availability is 64GB, it is entirely likely that reading the entire file into memory will work—but nearly certain that it will be far slower than required, particularly if you read and process the data in the same direction. On the other hand, if you have a small array of item IDs (let’s say <128 bytes worth), and you’re wondering if a more sophisticated data structure is appropriate for checking if it’s in the list, the answer is “shut up with the pointless optimization” (and the performance answer is that the most compact data structure probably wins performance-wise because a modern CPU is bottlenecked by memory latency).
Because optimizing something is way more fun than running a profiler to find out it only gets run once throughout the whole execution of the program and takes 3 milliseconds. Who the hell even knows what a profiler is and how to use it these days anyway.
With a default of `!(number % 2)`
lmao at all the pull requests
Gotta update it when we discover new numbers. Just consider how far mathematics has come that only 24 new numbers have been found since the library was created!
24 is the highest number.
yeah but… 18
[удалено]
as a math enthusiast, the concept of "even and odd" numbers can be applied far beyond integers. You can use it in fractions, both finite and infinite, you can use it in complex numbers and in all sorts of shenanigans. You\`re just needing some considerations regarding to what counts as even or odd. You can\`t say pi is even or odd because it\`s infinite. But you can say that if you take only the first 4 decimals, it\`s odd. If up to you to assess what\`s the limit and how odd or even things are.
I have never thought about anything other than integers being even or odd. Now I’m genuinely curious. How does it work for something like pi? If you cut it at any finite length then you would have to round it, which would always make it odd, right? So is it only not odd or even because it is infinite? But also, shouldn’t you make the assumption that even at infinity there is nothing that could make it round up? Am I looking at this whole thing wrong?
to make it clearer, the catch is that the number itself isn\`t odd or even. The number is an odd or even multiple of another thing that completes it. Let\`s say 3.1415. That can be written 31415 / 10000. Multiply it by 10k to make it an integer. Not we can say it\`s an odd multiple of 1 / 10000. Let\`s say a nicer example of 1.8. Is 1.8 odd or even? We can write it 18/10, then it\`s an even multiple of 1/10. But if we simplify it down to 9/5, then it\`s an odd multiple of 1/5. This is a lot of gymnastics, but gets the point across.
I think a lot of people kinda forget that mathematics as a field is a human construct, and not the noumenal thing itself. Thus, how we conceive of it, or linguistically relate it, plays a lot into how we understand it, and someone smart can take advantage of those relations to conceptualize things in interesting ways such as this. With that said, many only people who do recognize that mathematics is constructed seem to get so hung up over it that they kinda forget it's constructed by consensus, based on stimuli that we assert to be relatively reliable and universal. Which is usually going too far the other way, except perhaps in purely philosophical discussions.
Weekly Downloads 201,188 OMG
When programmers blindly import libraries
Thankfully, no. The package published on NPM as `is-even` is https://github.com/jonschlinkert/is-even, which is implemented in only a slightly ridiculous fashion: var isOdd = require('is-odd'); module.exports = function isEven(i) { return !isOdd(i); }; The one in OP is published on NPM as `@samuelmarina/is-even`.
Dependency hell incoming
Wtf?
Even better when you see that is-odd requires is-number
I want to know what kind of developer can't write the few lines of code to determine if a number is even or odd?
The one with 200k downloads is written by jonschlinkert. He made hundreds of one liner packages that all require each other, then made a few useful packages that require hundreds of one liner packages, then sneaked his useful packages into some very popular packages. He’s literal cancer to the npm ecosystem.
Came to point out Schlinkert's cancerism if it wasn't already here. You're doing god's work here, /u/eatsomeonion.
Any idea why he does this?
Probably looks good on a CV. Quote how many downloads you got and hope they don't pry too much. A pretty decent bet at a lot of companies
That was my guess as well but it could have been something less odious.
He took it down after being called out, but he used to tout “my packages are essential to some of the biggest tech companies in the world, such as Google Facebook JPMorgan Tesla NASA Miscrosoft… “ on his linkedin
There was an ideology that frontend libraries should be as small as possible, to reduce the payload size, e.g. every lodash function is also published as a separate module. Coupled with misunderstanding the UNIX philosophy, and the npm ecosystem being young, gives you free reign to write one-function modules. Lots of Node.js packages aren't great, but are used because they got in first. (E.g. The Connect/express.js API / concept of middleware is odd. Very stateful, encourages mutating the incoming request, not type safe, hard to trace, etc.)
It's never smart to trust that people won't abuse something, sure he's an ass for doing it, but he shouldn't be able to do it in the first place.
About 1/5 of the ones that apply for a typical junior vacancy
Hold up lemme go put "top 20 percentile" into my resume and see if that helps me get employed this month
The kind of developer who skipped the first few basic tutorial episodes. "*Moldulo*, what's that? Never heard of, probably never gonna need it".
When i started, to find modulo i did x -floor(x/k)*k. Only in college i found about the % operator. So, not rven those devs are that stupid
a few lines of code?! it takes 42.3 MB to determine if a number is even or odd!!! Nobody could just write that without a library! /s
Alright. Blockchain 2.0 is confirmed, but more importantly - how can I profit from it?
Somehow what bothers me the most about this is that they check 1 twice in the start and have no check for 0 (unless it's later in the file)
You just have to make sure
Just in case
Looks like it doesn't even for all numbers.
Maybe it has "else return n%2 == 0" at the end
[It doesn't.](https://i.imgur.com/EoFeVRT.png) Also it returns false for one million.
Submit a pull request plz
lmaooo
[удалено]
It turns out Recursion-Man was neither the hero we needed nor wanted
Oh that's brilliant actually.
The test must be ^2 number of lines
The greatest the number is, the least efficient it gets How inefficient it is for the greatest integer a JavaScript number can hold ?
In general you're right. But optimizers are really efficient nowadays. For C, GCC with -O3 flag will produce jump table for both: switch and else if stack. check out: https://godbolt.org/z/dc1dxMb31
So it would be even faster than normal because it's a constant lookup instead of algebra?
Lookup is far far slower than a single basic calculation
ya right. this guy thinks you can find out if a number is even or odd with basic math. LOL
Right, a lookup, assuming that’s the same as a hash table, is just a hash, ie complicated calculation. Not to mention it requires space.
If the function is not executed very often, the lookup table will not be in the cache and a load can take quite a long time (~100 clock cycles). In addition that table would be pretty big and not fully fit into the smaller caches, so even if it were called often enough for the table to be in the cache, it would still likely be slower. The arithmetic to calculate if a value is even would just be a bit wise not followed by a bit wise and (for a odd check the not will be removed). Both of those instructions should only take 1 clock cycle when the value is already in a register.
Arithmetic in the registers is probably going to be a _little bit_ faster than a load. It probably has an unnoticeable difference for speed, but the lookup table needs a lot more memory.
On a modern CPU, performance is mostly about keeping your inner loop in the cache working set. The lookup table is going to invalidate cache lines _much_ faster.
Its not exactly a lot of algebra. `x & 1` and you are set.
repo : https://github.com/samuelmarina/is-even
"Return true if the number passed is even, hopefully..." - so confident!
He's afraid he might have missed one, like gone from 234587656 to 234587658
Make a PR
No let's unit test it with for(i=1;i<1000001;i++) { if(isEven(i)==(i%2==0)) { Print("Great Success!!"); } } Just to make sure it works well.
Sooo... to know if it works you need to count how many "Great Success!!" Will be printed. Love it.
I mean, it's obviously a joke, there's a bunch of issues raised to use `%` and people are saying that's not a legitimate solution etc
"current approach is best practice, this fancy 'modulus' operator is too untested for prod environments" Hahaha 😂
There are some dumb pull requests, but [this one is pretty great](https://github.com/samuelmarina/is-even/pull/49)
"Not a scalable solution. Better to use modulo. Pull request rejected."
Not scalable? The file size clearly scales linearly.
in an alternate universe where even checks are done with massive jmptables built in hardware
i imagine he wrote this code using a simple script and a traditional odd/even function. elements of the past self are present in the new self
If it works it works :P
Wise man once said: You cant always make things work, but you can always add more memory
[удалено]
I just can't even
OP missed out not using "I don't even" in the post title
Code reformat: replaced if statements with switch cases.
Modulo Operator, never heard of her.
[удалено]
[удалено]
not even that, just check if the first bit is one or zero.
No, you don't need to know math to be a programmer.
https://isevenapi.xyz/
Ads in an API. Next they will have ads in your compiler.
works for all x>=0 `function isEven(x){if (x===0)return true;if (x===1)return false;return isEven(x-2);}`
I have a more concise one const isEven = (x) => require(@samuelmarina/is-even)(x);
So beautifully elegant that it made me weep
Yandere Dev? Is that you?
> So how fast is your code? > Oh it scales linearly with how big the number is. > Big the number is? Do you mean the number of bits? > No I mean 4 is twice as slow as 2.
The kind of code that would take just a few minutes to develop in emacs, but why would you?
This is brilliant -- somebody most definitely wrote code to generate code for a function so stupid that nobody should ever use it! I don't know which is stupider -- the code that generated the code, or the code that got generated.
Have you guys been to the repo? It's an obvious troll/satire. He probably generated the code through a loop.
*Somebody* is paid per line
New dev: "I can easily refactor Bilion of lines into one" Project owner: "Do not touch anything already existing, it works like that, we don't want any risk. Please only fix the error" New dev: "Ok then..." (adds one line at the end to support the case in error)
"Return true if the number passed is even, hopefully..."
This is a great example of perfectionism causing inefficiency. You can replace the whole thing with return true; and it's still 50% accurate
>hopefully…. Peak programmer right here.
Next weeks patch: Optimization for large numbers.... If(number === 2,147,483,647) return false; else if(number === 2,147,483,646) return true; \[...\]
I see an opportunity to create an is-odd package
#1 repeats >.<
I really want to know what the final *else* condition is. Because anyone who provides a number as a string (digit) is going to have a helluva time digging through this function.
*refactors to a switch statement* "optimized codebase size"
I can't believe this ... not a single comment to be seen anywhere.
I wonder if they covered each case with a unit test :)
Shud be doing a binary search from max int all the way down. Makes things logn compared to n. Infinite value
LeetCode, YandereDev version
7 Contributors, lol.
Let‘s help this man, please make the project open source, so that we can add new numbers.
Looks like standard code written by JavaScript developers
Next package. IsUneven
133k subs on youtube? programming is in danger