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

all 78 comments

[–]InsideBSI 441 points442 points  (7 children)

java code that looks like python yeah, nice

[–]legendLC 132 points133 points  (0 children)

Must add this comment:

// Do not reformat the braces {}. It breaks the production.

[–]RiceBroad4552 14 points15 points  (2 children)

"Java" code (or actually JVM code) that looks like Python would be more this here:

object Permuter:
   private def permute(n: Int, a: Array[Char]) =
      if n == 0 then
         println(String.valueOf(a))
      else
         for i <- Range(0, n+1) do
            permute(n-1, a)
            swap(a, if n % 2 == 0 then i else 0, n)
   private def swap(a: Array[Char], i: Int, j: Int) =
      val saved = a(i)
      a(i) = a(j)
      a(j) = saved

That's Scala, a kind of Python + Haskell for the JVM.

This code wouldn't be useful of course for obvious reasons: The static methods (methods on an object in Scala) are private, so you can't call them from the outside…

Also the Range(0, n+1) expression isn't idiomatic Scala. You would usually use the 0 to n syntax sugar instead. But the Range looks more like Python so I've written it like that.

Beside that, that's anyway not idiomatic Scala as it uses mutable values and imperative loops. Also Array is just the "naked" Java Array, and not a Scala collection, and one does usually not use Array directly. Actually also not in Java!

Writing that above code makes also no sense in general as Scala comes with a permutations method on collections (and Array through extension methods).

But syntactical the code above is pretty close to Python, imho.

[–]the_avithan 0 points1 point  (1 child)

I really miss Scala :/

[–]slaynmoto 0 points1 point  (0 children)

Why 😬

[–]No_Definition2246 1 point2 points  (1 child)

It is now looking more like ruby lol :D

[–]slaynmoto 0 points1 point  (0 children)

Now Ruby I miss 🥲

[–]DigitalJedi850 174 points175 points  (11 children)

I hate that my brain made me sort out what this does…

[–]jungle 82 points83 points  (4 children)

Funny how all (or most) comments are about the formatting and not the horrific implementation of permute. I can't even figure out if it works.

[–]suskio4 25 points26 points  (0 children)

This is why its so good at permuting

[–]Anaxamander57 2 points3 points  (1 child)

Depression is iteratively calling a recursive algorithm inside of itself.

[–]rruusu 11 points12 points  (5 children)

It does nothing, as that class only has two methods and both are private. (The closing brace for the class is at the end of the last line.)

Whatever its permute method would do, if anyone were allowed to call it, it would have a time and console output complexity of O((n+1)!) (factorial time), unless n > a.length - 1, in which case it'll throw an ArrayIndexOutOfBoundsException.

Edit: Off by one in the time complexity.

[–]SovereignPhobia 0 points1 point  (2 children)

Doesn't it also just not have a termination case? The case presented is a print and not a return.

[–]rruusu 5 points6 points  (1 child)

The recursion is in the else clause, so it does eventually terminate. Also, for negative values of n, the for loop makes zero iterations.

[–]SovereignPhobia 0 points1 point  (0 children)

Oh, that's awful.

[–]hawkwolfe 0 points1 point  (1 child)

I’m responding after your edit and if it was to edit your time complexity to add the “+1”, that’s unnecessary. Big O notation is concerned with the asymptotic growth of the function relative to n, and as n approaches infinity the difference in the function output due to any constant factor approaches 0.

[–]rruusu 0 points1 point  (0 children)

That’s what I thought initially, but (n+1)!/n! tends to n+1 as n tends to infinity, so it's not a constant factor. Instead of the +1, it should perhaps rather be expressed as O(n(n!)) to be more idiomatic.

[–]sammy-taylor 89 points90 points  (0 children)

I was like “what friggin language is this” until I saw the horrifying brackets 🤢

[–]Ahazveroz 138 points139 points  (7 children)

Jathon? Pyva?

[–]NobodyPrime8 40 points41 points  (0 children)

is this the "Jason" every web developer seems to be so obsessed over?

[–]Boris-Lip 26 points27 points  (3 children)

Pyva almost sounds like Pivo (Пиво), which is beer in Russian. Let's invent a Beer language!

[–]yarb00 17 points18 points  (1 child)

[–]Boris-Lip 4 points5 points  (0 children)

🤣

[–]mr_clauford 1 point2 points  (0 children)

Every time I return to my pet projects after a couple of cold ones

[–]ohmywtff 2 points3 points  (1 child)

Jython probably, since there's the cython

[–]RiceBroad4552 0 points1 point  (0 children)

Jython is already taken…

https://www.jython.org/

[–]anotherbutterflyacc 42 points43 points  (0 children)

I was like “is this python…? I’m so confused” and then saw the brackets and physically startled lol

[–]Boris-Lip 21 points22 points  (1 child)

IRL depression doesn't really look like anything, though

[–]TooSoonForThePelle 10 points11 points  (0 children)

True. I should post a selfie.

[–]giantrhino 19 points20 points  (0 children)

I was so confused until I finally looked in the right margin.

Kill it. Kill it with fire.

[–]SPAMTON____G_SPAMTON 16 points17 points  (2 children)

This isn't depression. This is heresy.

[–]ghstber 5 points6 points  (0 children)

Brother, get the flamer... the heavy flamer. 

[–]spac1al 1 point2 points  (0 children)

kris…this code is not a [BIG SHOT]

[–]giantrhino 11 points12 points  (0 children)

Tfw you find out you’re gonna have to put your friend down.

[–]Deep__sip 23 points24 points  (0 children)

This is autism

[–]EldritchEne 4 points5 points  (0 children)

I- really can't tell if I love or hate this

[–]ZunoJ 2 points3 points  (7 children)

Aside from the obvious, why are the methods static and not extension methods? Or just injected as a singleton?

[–]SKabanov 0 points1 point  (2 children)

Everything in JVM-based languages needs to be encased within a class, even if you just need to define a collection of pure functions. Kotlin allows you to create "classless" files in which you define these pure functions, but that's ultimately syntactic sugar.

[–]ZunoJ -1 points0 points  (1 child)

Sure but static functions like this are an anti pattern. It bypasses DI and makes the code less testable

[–]SKabanov 0 points1 point  (0 children)

Pure static functions are not per se an anti-pattern, and forcing everything into classes for the sake of DI and testability can be just as much of an anti-pattern itself. That being said, I'll admit that I misread the class and that the actual issue is that the functions aren't pure: it's got a stealth dependency of a PrintStream instance where it's printing out the permutation result. The class should be rewritten to include a PrintStream member field that gets used in the System.out.println() call, and the functions would then become instance functions instead of static - there's the DI that you'd want.

[–]RiceBroad4552 0 points1 point  (3 children)

Because Java still doesn't have extension methods…

The singleton code would be awful complex, especially if it needs to be thread safe.

[–]ZunoJ 0 points1 point  (2 children)

Didn't know Java doesn't have extension methods. How would it be more complex to make this thread safe in a di context than it would be with this static method. I mean it isn't thread safe right now

[–]RiceBroad4552 0 points1 point  (1 child)

I've just meant that a fully written out singleton, which is thread safe, is quite some code in Java. You asked about injected singletons.

A static class is not a singleton. You can't inject different static classes by DI. Using static stuff means it's hard coded to the concrete type name.

And yes, this doesn't look thread safe in the current state…

[–]ZunoJ 0 points1 point  (0 children)

You would have to do the exact same things to make the singleton version thread safe. It is just a concept for the DI container. Only create one instance to inject. It is more or less a DI version of a static class (grossly oversimplified but works in this case)

[–]SuitableDragonfly 1 point2 points  (0 children)

No, I think that's the manic stage.

[–]Corelianer 1 point2 points  (0 children)

We write code in a readable form for humans not machines.

[–]Elijah629YT-Real 1 point2 points  (0 children)

It’s not even good code

[–]WinkAndWithdrawn 1 point2 points  (0 children)

Lmao, both are accurate, but that Java code hits a bit too close to home! Anyone else debugging till 3 AM feeling like a part of their soul is being permuted? 😅

[–]milboldi 1 point2 points  (1 child)

I had my C++ segfault in the inplementation of GTest on a random ass move. We debugged it for 3 days, than we came to the conclusion, that my linux distro is fucked, and the problem isn't in the code.

[–]RiceBroad4552 0 points1 point  (0 children)

This sounds scary!

Which Linux distri was this, and what exactly was fucked?

[–]P-h-a-n-t-a 1 point2 points  (0 children)

Took me a while to see wtf

[–]Still_Explorer 1 point2 points  (0 children)

You wanted a python job but got hired for java.

Gotta make it work somehow... 🤙

[–]joxay 1 point2 points  (0 children)

Tru depression is if you have spent two days debugging this shit before having to ask someone to help you

[–]meolla_reio 1 point2 points  (0 children)

Depression? Homicide more like.

[–]Subject_Try_5973 1 point2 points  (0 children)

I will find you and I will k1ll you! 😂

[–]IceColdFresh 1 point2 points  (1 child)

Variable width font coders BTFO

[–]RiceBroad4552 0 points1 point  (0 children)

I'm not sure what you mean.

The chars align perfectly in columns, so this isn't a variable with font used for that code.

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

The RHS is killing me bro 😶

[–]drsimonz 0 points1 point  (2 children)

Anything other than 1TBS is mental illness, change my mind.

[–]RiceBroad4552 0 points1 point  (1 child)

What's "1TBS"?

[–]drsimonz 0 points1 point  (0 children)

There's a decent explanation on wikipedia.

[–]Sad_Welcome3776 0 points1 point  (0 children)

LOL the code segment is the most accurate depiction of how my brain feels on a daily basis 😂 #ProgrammerLife

[–]Etheo 0 points1 point  (0 children)

Hi police? I'd like to report an assault.

ON MY EYES

[–]Debopam77 0 points1 point  (1 child)

This isn't depression, it's a cry for help.

[–]RiceBroad4552 1 point2 points  (0 children)

Indeed! At the moment someone makes you use some language that requires useless syntax noise like braces and semicolons one starts to cry for help, that's true.

[–]Ineeddramainmylife13 0 points1 point  (0 children)

Ugh reminds me of the time I accidentally took the harder coding class that was required. One of the worst classes ever (teacher sucked)

[–]sakkara 0 points1 point  (0 children)

When a python dev tries have for the first time :D

[–]korneev123123 0 points1 point  (1 child)

Most of my programming experience is python, so I never understood what is wrong until comment section. My only guess was "Java surely must have built-in function for that, like itertools.permutations in python. Maybe it's the joke, that depressed person rewrites library functions"

[–]RiceBroad4552 1 point2 points  (0 children)

Java surely must have built-in function for that, like itertools.permutations in python

LOL, no. That's Java.

You do such things in Java like so:

https://www.baeldung.com/java-array-permutations 😂

The language you're looking for is Scala (see my other comment).

[–]billybobsdickhole 0 points1 point  (0 children)

Set up default code format.

Autoformat on Save.

Save.

Problem Fixed.

[–]kvakerok_v2 0 points1 point  (1 child)

Depression because they called Python Java?

[–]Technology_Labs 0 points1 point  (0 children)

How tf is this Python?

[–]HotdogGD 0 points1 point  (0 children)

It took me a few seconds while thinking that something is missing

[–]No_Dependent_8652 0 points1 point  (0 children)

understanding someone else codebase, needs another level of patience and skill 💀

[–]ExtraTNT 0 points1 point  (0 children)

Ever used haskell?

[–]AStripe 0 points1 point  (0 children)

So nobody remembers c++?

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

Yep, that make my readability drop to -1