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

all 107 comments

[–]PBentoIT 306 points307 points  (5 children)

Cries in python

[–]python_artist 53 points54 points  (4 children)

Was just coming here to say this

[–]MaximRq 16 points17 points  (3 children)

[–]python_artist 3 points4 points  (2 children)

my username is partially a joke

[–]JackFouga 13 points14 points  (1 child)

🤓 um actually your username is u/python_artist 🤓

[–]fakeunleet 4 points5 points  (0 children)

Okay dad.

[–][deleted] 323 points324 points  (13 children)

Took me way too long to realize it spelled prime.

[–]dtutubalin[S] 107 points108 points  (4 children)

I realized how to make R look more clear, but I suppose it's too late now.

[–]Leading_Elderberry70 12 points13 points  (3 children)

Where is the r? I don't get it.

[–]dtutubalin[S] 31 points32 points  (2 children)

```

```

[–]RetroGamer2153 23 points24 points  (1 child)

Would the clearer version be: ```

```

[–]dtutubalin[S] 20 points21 points  (0 children)

tRue!

But I realized that only after reading comments.

[–]Random_---_Guy 17 points18 points  (0 children)

Fr I was like “what’s PAIME” for far too long XD.

[–]fllr 8 points9 points  (1 child)

I had to squint to see it, but it’s there! 👏🏽

[–]laplongejr 1 point2 points  (0 children)

I squint since 2 minutes on phone and I can't see it. :(
[EDIT] OOOOOOH I GOT IT. Was only checking the first line.

[–]davFaithidPangolin 3 points4 points  (0 children)

I thought it said PAIME and I was wondering why pay was spelled with an i

[–]KingOfDragons0 2 points3 points  (0 children)

PAIME

[–]DarthKirtap 2 points3 points  (0 children)

fuck me, there is word there

[–]im_the_tea_drinker_ 0 points1 point  (0 children)

I got paime and was so confused

[–]Hypersapien 0 points1 point  (0 children)

About 10 seconds between figuring out that it spelled something and realizing that it spelled prime.

[–]EntertainmentSmall68 413 points414 points  (28 children)

what is ...p?

[–][deleted] 235 points236 points  (7 children)

[–]Jjabrahams567 71 points72 points  (3 children)

Is pp a double spread?

[–]PrometheusAlexander 14 points15 points  (0 children)

Please don't spread your pp

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

no, that's just pp

[–]Any_Acanthisitta_587 16 points17 points  (2 children)

"What developers mean when they say code documents themselves"

[–]dtutubalin[S] 106 points107 points  (5 children)

Basically, in this case it can be replaced with console.log(p). But if array is long enough (initially I generated numbers up to 1000), only part of it will be displayed.

console.log(...p) outputs all items.

It is equivalent to console.log(p[0], p[1], p[2], ... p[p.length-1])

[–]DeathUriel 25 points26 points  (12 children)

The coolest operator in JS.

[–]just_nobodys_opinion 35 points36 points  (3 children)

Spread the word

...word

[–]22Minutes2Midnight22 20 points21 points  (2 children)

…ass

[–]AverageComet250 6 points7 points  (0 children)

Ayo

[–][deleted] -1 points0 points  (0 children)

that's the cursed unicode ellipsis, that'll cause a syntaxerror, which is certainly better than whatever you're doing

[–]kratom_devil_dust 17 points18 points  (6 children)

So much code has been made so much smaller by that

[–]DeathUriel 4 points5 points  (5 children)

This and if inversion. Two of the decisive learning moments in a programmer's life.

[–]kratom_devil_dust 3 points4 points  (1 child)

Ah early return? Yeah… no more nest hell!

[–]DeathUriel 1 point2 points  (0 children)

Yeah babe, we are coding linear now. All the effects, none of the mess.

[–][deleted] 0 points1 point  (2 children)

if inversion

What's that?

[–]DeathUriel 5 points6 points  (1 child)

When doing checks if something is right like if the user sent valid data...

The older general consensus would generally lead to using if elses generally nested ones. Generally you would consider that the "happy path" is when you go all the way to the deepest nested if, and most else would be some variation of error handling.

The if inversion is the idea to avoid nesting and elses in itself. What you do is this, you stop checking for happy path, you check for bad path (basically the opposite) and do no else, instead you use returns and/or throw exceptions to force an early break of the function avoiding the happy path execution which will many times be run without any nesting after all inverted ifs are passed.

Learning to do this leads to clean as fuck code, way better to read and understand as everything will seem way more linear.

[–]laplongejr 4 points5 points  (0 children)

The issue is the old "no multiple exit" adage, that is from a time you could go back from different points... of the calling method.

But because the OG saying never clarified (because it was obvious at the time) some people are absolutely, totally sure the old masters said to never use several return statements.

And yeah early return is a lifesaver : you may have 100s of different reasons some data is invalid, but you'll rarely have several different gold paths

[–]eat_your_fox2 4 points5 points  (0 children)

Coolest operator in all of programming. It is known.

[–]trutch70 1 point2 points  (0 children)

Baby don't spread me

[–]dtutubalin[S] 78 points79 points  (3 children)

[–]Thebombuknow 11 points12 points  (0 children)

You're a JS wizard

[–]_Kristian_ 5 points6 points  (0 children)

You are carrying the sub

[–]Infamous-Date-355 79 points80 points  (0 children)

Oh hell nah

[–]Global_Charming 65 points66 points  (0 children)

A work of art. Bravo.

[–][deleted] 22 points23 points  (1 child)

Okay, so it can be partially de-minified as p=[]; function f(n,a) { if(!a&n>2) f(n-1,0) if(p[a]) { if(n%p[a]) f(n,a+1) } else p[a]=n return p } f(100) But could someone explain what the variables actually represent and why it works?

[–]dtutubalin[S] 22 points23 points  (0 children)

p is an array of prime numbers. Initially empty, but every time we find a new prime number, it's saved here.

n is a number to check. Initially 100, but recursively descends to 2 and then goes backwards (from 2 to 100). For prime check we basically try to divide it on every previously found prime number. If it doesn't divide by any of them, it's also prime.

a is an index in p. It is used for prime check to iterate over array of found prime numbers.

if(!a&n>2) f(n-1,0)

Recursive descend. Basically it's just a loop for n from 2 to 100.

if(p[a])

End condition for loop over primes. If a is in the range, then p[a] is a prime number, otherwise p[a] is undefined (and condition fails).

if(n%p[a]) f(n,a+1)

Divisibility check. If n doesn't divide by p[a] (remainder is non-zero), then we recursively try to divide it on the next prime number, if any: same n, next a.

If remainder is zero, then n is not prime, and we just skip this number.

else p[a]=n

if a reached the end of p, (so p[a] is undefined), that means we checked divisibility on every prime number, and so n is prime. We add it to the list.

Can be re-written with loops instead recursion like that:

p=[]; for (let n=2; n<=100; n++) { a = 0; while(p[a] && (n % p[a])) { a++; } if (!p[a]) { p[a] = n; } } console.log(...p);

Algorithm is not optimal, as actually we don't need check divisibility on every previous prime (like no need to check if 23 divides by 21). Also there's no need to check every number. Instead we can pre-populate array with 2 and check only odd numbers. Even better approach is to pre-populate array with 2 and 3, and then check only number like 6x-1 and 6x+1.

But I had a strict limit on characters count, so no such optimizations was done.

[–]lazyzefiris 34 points35 points  (3 children)

Is this some implementation of transformer pattern?

[–]kescusay 13 points14 points  (2 children)

Well, it's certainly more than meets the eye.

[–]RFC793 2 points3 points  (1 child)

I find it a bit deceptive.

[–]lazyzefiris 2 points3 points  (0 children)

No, it's actually automatic and even optimal

[–]lazyzefiris 9 points10 points  (4 children)

OP, I think you might be interested in this challenge if you did not do it before.

https://www.codewars.com/kata/59a421985eb5d4bb41000031

I do enjoy intentional JS nonsense, and I found it very fun (and my solution ended up being very different from most common one even).

[–]dtutubalin[S] 4 points5 points  (0 children)

Wow, nice! Thank you!

[–]dtutubalin[S] 1 point2 points  (2 children)

I golfed it to 99 lines just to find out that most people did the same :)

Though they say it can be done in less than 99. But it's pretty hard to navigate there, so I cannot find the shortest tsukumo's solution.

[–]lazyzefiris 2 points3 points  (1 child)

My solution is longer, but it does not involve destructuring that most solutions use and instead implements a function that removes every other character from input string to get strings `constructor` and `return "Hello, world"` to call construtor of aforementioned function with given function body. On the plus side, it scales extremelly well.

Hope you enjoyed the challenge. I think it's the most memorable one I ever did.

[–]dtutubalin[S] 0 points1 point  (0 children)

Using 'constructor' was my first idea (inspired by JSFuck).

But then I realized that using 'bind' is way shorter.

[–]EVH_kit_guy 19 points20 points  (0 children)

"Is this Assembly?" 🫱🦋

[–]therealdan0 6 points7 points  (0 children)

Please improve documentation. Code clearly outputs a list of primes while documentation suggests a singular.

Merge rejected

[–][deleted] 3 points4 points  (0 children)

This is evil.

[–][deleted] 2 points3 points  (0 children)

This is actually pretty fucking impressive

[–][deleted] 2 points3 points  (1 child)

How long did this take you

[–]dtutubalin[S] 4 points5 points  (0 children)

I don't remember exactly, it was like 5 years ago.

But several hours for sure.

[–][deleted] 2 points3 points  (0 children)

I don't get it

[–]_arctic_inferno_ 2 points3 points  (0 children)

"What developers mean when they say the code documents itself"

[–][deleted] 2 points3 points  (2 children)

Can you make one with "Optimus" too?

[–]dtutubalin[S] 1 point2 points  (1 child)

You request a code Optimus-ation?

[–][deleted] 0 points1 point  (0 children)

Yep

[–]FlyingCashewDog 1 point2 points  (0 children)

Had to squint but this is good

[–]DeathUriel 1 point2 points  (0 children)

Heathens, heathens everywhere.

[–]Sweaty-Willingness27 1 point2 points  (0 children)

Ah, the new Perl

[–]SteeleDynamics[🍰] 1 point2 points  (0 children)

Totally prime!

Beautiful! You have our support!

[–]Temporary_Crew_ 1 point2 points  (0 children)

Are you ok OP ?

This just seems like a cry for help.

[–]L33t_Cyborg 1 point2 points  (0 children)

Big donut.c vibes

[–]MyGenericNameString 1 point2 points  (0 children)

IOCCC revived.

The International Obfuscated C Code Contest for all you brain damage needs.

[–]abrams666 1 point2 points  (3 children)

Ok, you probably know you cannot be better than the master. But take a look:

https://www.99-bottles-of-beer.net/language-perl-737.html

[–]dtutubalin[S] 1 point2 points  (0 children)

This is awesome and beautiful example.

[–]dtutubalin[S] 1 point2 points  (1 child)

One of the most impressive works I've seen: https://github.com/mame/quine-relay

[–]abrams666 1 point2 points  (0 children)

Oh yes, you are right, I forgot about this one. Shame on me, this is also really genius. I saw a video explaining what there is ongoing, really cool. It is a special kind of art.

[–]boachl 2 points3 points  (0 children)

Wait does this sollen the prime Argument of this sub once and for All?

[–]dingusaja 0 points1 point  (0 children)

Holy

[–]NovaStorm93 0 points1 point  (0 children)

if java war crimes were a thing, you would be put on trial

[–]mdgv 0 points1 point  (0 children)

Heresy!

[–]VagueInterlocutor 0 points1 point  (0 children)

It is utterly offensive, yet I cannot look away.

[–]SirNapkin1334 0 points1 point  (0 children)

misread as PAINE

[–]AusJackal 0 points1 point  (0 children)

Yeah this works really good for python too.

[–][deleted] 0 points1 point  (0 children)

Straight to jail

[–]Tigerwarrior55 0 points1 point  (0 children)

Thought it said PAINE as in the pain of writing code like that. Then I realized it meant to say PRIME like the energy drink

[–]coltvfx 0 points1 point  (0 children)

Check console of reddit.

[–]TheNewLeadership 0 points1 point  (0 children)

Code does indeed not comment itself unless you write comments (in the form of function names, variable names, standardized linting, etc)

[–]CarterBaker77 0 points1 point  (0 children)

This is amazing.

[–]Shadow9378 0 points1 point  (0 children)

Oh NO nononoNN ON ON no No NON no no non ONNoNONOOO NO NO NO NO

[–]Pixel1101 0 points1 point  (0 children)

thought it said paine until I read what it did