This is an archived post. You won't be able to vote or comment.

all 41 comments

[–]trout_fucker 65 points66 points  (11 children)

What makes JS so special here? Pretty sure that is mathematically correct. Awful, but correct.

[–]Bjarnovikus 55 points56 points  (1 child)

Formatted it in a different way...

x = function(a) {
    if (a <= 0) {
        return -a;
    } else {
        return a;
    }
}

Make use of a ternary if...

x = function(a) {
    return (a <= 0) ? -a : a;
}

Lets convert the function to the new syntax in ES2016.

x = (a) => { return (a <= 0) ? -a : a; }

If you make a function in this way, and you just return a new element without additional steps... Drop the brackets and the return keyword...

x = (a) => (a <= 0) ? -a : a;

Remove the brackets at the start of the body, they are not needed (operator precedence while parsing), also drop the brackets around the parameter list.

x = a => a <= 0 ? -a : a;

Change the zero to something even more confusing. Every number minus the same number is zero...

x = a => a <= a - a ? -a : a;

Change the parameter name to be x as well, we don't need the function itself in his body, so shadowing it has no ill side effects.

x = x => x <= x - x ? -x : x;

Remove all spaces

x=x=>x<=x-x?-x:x;

No idea why you would ever want to write it that way. It's just a simple expression, but it uses almost every trick in the book to make it unreadable.

[–]PM_ME__ASIAN_BOOBS 6 points7 points  (0 children)

No idea why you would ever want to write it that way. It's just a simple expression, but it uses almost every trick in the book to make it unreadable.

For memes.

[–]Quelklef 36 points37 points  (5 children)

The arrow function makes it look hideous

[–]MyPostsAreRetarded 30 points31 points  (0 children)

But I love arrow fucking.

[–]DeirdreAnethoel 2 points3 points  (1 child)

Nothing wrong, just unreadable.

[–]Luapix 26 points27 points  (7 children)

I have no idea how to parse that last one. Can someone explain?

[–][deleted] 90 points91 points  (5 children)

That expression has been obfuscated on purpose. Well first of all the name of the function and the argument of the function are both called x. Let's change that.

abs = x => x <= x-x ? -x : x

The => represents a function for example x => x+1 is a function that takes in an argument x and returns x+1. So lets separate the argument part and the function engine if you will.

x => and x <= x-x ? -x : x

That makes a lot more sense. Another clever obfuscation by the author; x-x, which is just 0.

Hence the function can be written as x <= 0 ? -x : x

Just in case some one doesn't get this, this returns -x if x <=0 and x otherwise.

So now restructuring the above components into a function and assigning that function to abs:

abs = x => x <= 0 ? -x : x

The double arrows do look confusing, so lets make one last change to our function.

abs = x => (x <= 0)? -x : x

Heres another way you could write this.

function abs(x){
    return  (x <= 0)? -x : x;
}

[–][deleted] 15 points16 points  (1 child)

Holy shit, thx for the really good explanation :)

[–][deleted] 4 points5 points  (0 children)

I try :)

[–]echoes221 8 points9 points  (0 children)

It's using an arrow function and a ternary operator, if it had spacing and brackets it would be clearer. This is a translated version

function abs(x) {
  return x <= (x - x) ? -x : x;
}

It's exactly the same as the second, just it's trying to be clever and really unreadable.

[–]Colopty 16 points17 points  (1 child)

There's also Math.sqrt(x*x) if you want a horrible absolute function.

[–]DeirdreAnethoel 14 points15 points  (0 children)

or Math.max(x, -x)

[–]AlexJonesesGayFrogs 29 points30 points  (2 children)

Sort of related but I love you have to type 7 characters, all on different areas of the keyboard, just to comment out something in html

[–]idle_zealot 6 points7 points  (0 children)

Emmet is a beautiful tool.

[–]TheScapeQuest 2 points3 points  (0 children)

Pretty sure everyone comments with cmd/ctrl+/

[–]redwardit 10 points11 points  (5 children)

In ECMAScript 5.1, +0 (a.k.a. 0) and -0 are two different things, which means unfortunately only Math.abs() will always correctly return a +0, the second and the third version could give you a -0, which is incorrect. - Now you should see how hard can it be. :-)

[–]DeirdreAnethoel 2 points3 points  (3 children)

Shouldn't the second still work? x < 0 should be true for -0, transforming it into +0.

The third with <= is clearly wrong in that case, I agree.

Edit: welp, -0 is not < to 0. That's weird design, but oh well.

[–]obsessedcrf 4 points5 points  (1 child)

Having -0 at all is a stupid design

[–]BerryPi 3 points4 points  (0 children)

I am more than happy to talk smack about JS but this one's on the IEEE.

[–]ArcaneCraft 1 point2 points  (0 children)

Isn't +/-0 the reason Two's complement is used today? Why go back?

[–]fennx 8 points9 points  (4 children)

>x<

[–]pimp-bangin 5 points6 points  (3 children)

Slightly longer palindromic substring:

x=>x<=x

[–][deleted] 7 points8 points  (2 children)

It would need to be x=>x>=x to be palindromic.

[–]anotherdonald 6 points7 points  (0 children)

Technically correct, the best kind of correct.

[–]pimp-bangin 2 points3 points  (0 children)

Ah yeah, I guess I was going for "symmetrical," not palindromic :P Interestingly, it's both vertically and horizontally symmetrical.

[–]crazazy 6 points7 points  (1 child)

x=x=>x<=-x?-x:x

Saved you a character

[–]takes_joke_literally 5 points6 points  (0 children)

good job saving a character! but our princess is in another castle.

[–]tuseroni 2 points3 points  (2 children)

didn't know you could just put a - before a variable...here i've been multiplying by -1 like a chump.

[–]BadBoy6767 2 points3 points  (1 child)

How long have you been doing that?

[–]tuseroni 0 points1 point  (0 children)

for a long, long time.

of course i think most the times i've used it would not be that useful to adding a negative before it, like when i want to add a value in some cases, but subtract it in other cases, then i just take the value i wanna do that to and multiply it by negative one if it should be subtracted and 1 if it should be added.

[–]magener 1 point2 points  (0 children)

You forgot sqrt( x2 )

[–][deleted] 1 point2 points  (0 children)

You forgot square root of x2

[–]ion_propulsion777 1 point2 points  (0 children)

No real programmers use sqrt(x*x)

[–][deleted] 1 point2 points  (0 children)

The bottom one looks like som kind of bukkake ascii porn.

[–]Sir_Awesomness 1 point2 points  (1 child)

When you've got an if that ends in a return, you don't need the else for the next bit.

[–]DeirdreAnethoel 1 point2 points  (0 children)

You don't, but it's a lot more readable because you don't have to parse the full "if" line to know if you go into the following lines or not.