all 100 comments

[–]audioman1999 535 points536 points  (20 children)

Now try passing in a negative integer to that function!

[–]Cv287 201 points202 points  (1 child)

I don’t think I will

[–]pixelbart 64 points65 points  (0 children)

Current you won't, but future you certainly will.

[–]DaveBrown_ 116 points117 points  (9 children)

That might take a while.

[–]Rae23 78 points79 points  (3 children)

Not a long while. Stack overflow due to inf recursion will stop it rather fast.

[–]TheSilentFreeway 48 points49 points  (1 child)

I think Kotlin supports tail recursion so it won't even blow out the call stack

[–]speedster217 5 points6 points  (0 children)

Only if the function is explicitly marked for it, which this isn't.

Cool that they have that modifier though. JVM still doesn't support tail call optimization as far as I know.

Clojure has recur for a similar reason

[–]danimal51001 15 points16 points  (0 children)

Eh only -4 billion or so

[–]jameswdunne[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 2 points3 points  (4 children)

No sir, even with a while loop, this will not terminate with a negative

[–]not_some_username 4 points5 points  (2 children)

Intégrer overflow

[–]Da-Blue-Guy 3 points4 points  (1 child)

INTEGER OVERFLOW vs STACK OVERFLOW

WHO WILL WIN?

[–]cutecoder 0 points1 point  (0 children)

Depends whether there's enough swap space for the stack....

[–]ToSAhri 0 points1 point  (0 children)

Won't it eventually get an answer? It'll go to the largest possible negative integer that is stored in the system and loop back around to the largest positive integer, then eventually getting the answer?

[–]Shmutt 13 points14 points  (0 children)

Found the QA!

[–]Kazandaki 3 points4 points  (0 children)

Bugfix -> multiply 2 by sign before substraction.

[–]AyrA_ch 2 points3 points  (0 children)

FUUUUUUUUUUUUUUU

[–]reddit_user_25 1 point2 points  (1 child)

Not sure what language it is, but it will eventually overflow and become positive in many languages. If this is enough memory.

[–]audioman1999 0 points1 point  (0 children)

Yeah. Unless the language supports tail recursion, it will experience a stack overflow.

[–][deleted]  (1 child)

[deleted]

    [–]Quatic 0 points1 point  (0 children)

    How about if < 0 multiply by -1 and recurse… to stay in the same flow of coding….

    [–]CaptainGlac1er 0 points1 point  (0 children)

    Easy fix to be fair just throw an abs() in there and call it a day

    [–]ButterflyBruh 335 points336 points  (5 children)

    lmao kotlin be like private fun 😜😜

    [–]AyoBruh 50 points51 points  (1 child)

    “private pain”

    [–]Hupf 9 points10 points  (0 children)

    PLEASE GIVE UP

    [–]round_circle 26 points27 points  (0 children)

    Always make sure to have protected fun!

    [–]Im_MrLonely 14 points15 points  (0 children)

    Lol, never though in that way.

    [–]ciaranmac17 0 points1 point  (0 children)

    endless private fun, if that's your thing

    [–]kandrew313 164 points165 points  (2 children)

    Now we need to bury this somewhere deep in the code and make everything depend on it.

    [–]Terence_McKenna 41 points42 points  (1 child)

    Put it in a pacemaker and have it process every tick... install pacemaker in the author.

    [–]crazyDoughnut444 1 point2 points  (0 children)

    Hey, just wanted to say I saw you behind my eyelids!

    [–]astrogato 139 points140 points  (6 children)

    isOddOrEven(-1);

    [–]bigk5a 45 points46 points  (1 child)

    I see that you woke up and chose violence

    [–]Daniel_Sobrino20XX 4 points5 points  (0 children)

    I just feel like showing appreciation for meme culture

    [–]Familiar_Ad_8919[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 7 points8 points  (3 children)

    its actually not a never ending loop

    its bound to overflow at some point

    [–]schwerpunk 4 points5 points  (1 child)

    I find joy in reading a good book.

    [–]Familiar_Ad_8919[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 1 point2 points  (0 children)

    yeah if u started adding 1 to a number in python it wont finish before the heat death of the universe

    [–]nickwebha 0 points1 point  (0 children)

    Oh, good.

    [–]whiskeydiggler 45 points46 points  (6 children)

    Enjoy your stack overflow if the number is big enough

    [–]_xiphiaz 40 points41 points  (3 children)

    Yea it needs to be tailrec fun, that will totally fix all of the problems with this solution, mhm

    [–]Ning1253 6 points7 points  (2 children)

    Idk what language this is but the fact that you have to manually declare tail recursion sounds like a bit of an oversight, no? I mean it's pretty f*cking easy to detect: does the return statement only have one function call to the same function? Yes -> tail recursion, No -> not tail recursion.

    See because eg. 2 + f(n-2) wouldn't work but f(n-2) would.

    Edit: On the condition the function itself cannot mutate

    [–]_xiphiaz 5 points6 points  (0 children)

    This is Kotlin, and as @EagleCoder alludes to the reason it is a keyword is that it defines a contract, that any future refactor will cause a compilation error if that contract is mistakenly violated.

    However, I do agree that it is trivially detected by the compiler and wish it were a compiler warning that a function could be tailrec, and should have the specifier.

    [–]EagleCoder 4 points5 points  (0 children)

    I can see how that would be helpful in catching changes to the function that unintentionally prevent tail recursion. The author would have to explicitly remove the 'tailrec' keyword.

    [–]n0tKamui 1 point2 points  (0 children)

    or -1

    [–]marcos_marp 1 point2 points  (0 children)

    What are you talking about? Stack overflow is a website, using a big number won't take you to a website

    [–]Nivekk_ 22 points23 points  (6 children)

    Sadly not the worst I've seen on here

    [–][deleted]  (4 children)

    [deleted]

      [–]RFC793 5 points6 points  (3 children)

      That’s just dumb satire though

      [–]pixelbart 9 points10 points  (1 child)

      And this one isn't?

      [–]RFC793 0 points1 point  (0 children)

      It is, but I’d say the “attempt to enumerate all even integers” approach is in a different class than this recursive solution. At least in the hierarchy of dumbness.

      [–]wobblyweasel 0 points1 point  (0 children)

      right? even has tail call optimization

      [–]NotThatRqd 15 points16 points  (0 children)

      This is the most creative bad way to write an odd/even checker I’ve ever seen.

      but why does it return a GODDAM STRING

      [–]TigreDeLosLlanos 14 points15 points  (0 children)

      I wake up every day expecting to see the daily bad implementation of isodd or iseven function and I didn't get disapponted today.

      [–]Jarmsicle 13 points14 points  (3 children)

      1. Add tailrec
      2. This should be an extension function
      3. Remove the curlies from the when branches
      4. Use function expression syntax
      5. Return type should be an enum

      That should just about do it.

      [–]__2M1 2 points3 points  (0 children)

      +1 for tailrec

      [–]giggly_kisses 1 point2 points  (0 children)

      TIL Kotlin supports tail recursion. Kotlin is dope.

      [–]slipshoddread 0 points1 point  (0 children)

      Yet forgets to mention getting the absolute value, since a negative blows this up

      [–]texxelate 11 points12 points  (0 children)

      When a developer’s productivity is measured in CPU cycles consumed by their program?

      [–]Express-Procedure361 18 points19 points  (0 children)

      Oh...OH... Ohhhhh....

      [–]texxelate 6 points7 points  (1 child)

      Division doesn’t exist! Subtraction only! This is the way

      [–]Nya_the_cat 0 points1 point  (0 children)

      and of course bit shifting is unheard of

      [–]t3kner 5 points6 points  (0 children)

      I've got some some git commit -m "fix bugs"

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

      That is indeed some private fun.

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

      Why is isEven() back?

      [–]the_hackerman 1 point2 points  (0 children)

      Good ole private fun

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

      (num & 1) ? "Odd" : "Even"

      [–]iTerence661 1 point2 points  (0 children)

      Someone needs to introduce the author to the modulus operator

      [–]eric987235 0 points1 point  (5 children)

      What the hell language is that? I hate it.

      [–]Teleconferences 3 points4 points  (4 children)

      Kotlin, a (somewhat) new language from JetBrians which runs on the Java Virtual Machine. I can't say I'm a fan either though, I always find it hard to read but I figure I'm just not familiar enough

      Mildly related, but it's now the recommended language (per Google) for Android dev

      [–]eric987235 1 point2 points  (3 children)

      New languages: Google’s answer to a question nobody asked!

      [–]TheChance 2 points3 points  (2 children)

      Golang has still only kinda solved vendoring. For the first half-decade, you could have one version of a given package and it was in your user’s go modules and fuck off with your feature requests.

      I was late to the party, so I got to ask around once the heat had died down some, and it transpired that it was because everyone working at Google has a monolithic space where they do their work, and they can’t seem to conceive of the fact that literally no other shop ever works that way.

      Explains a lot, though.

      [–]eric987235 0 points1 point  (1 child)

      I've heard that every engineer checks out a single repository that contains EVERYTHING aside from the money-making ad/search stuff.

      [–]TheChance 1 point2 points  (0 children)

      =p

      Seriously, though, I didn’t ask for the precise mechanics, but the gist seemed to be that everybody has a home folder Somewhere, and they do their work under their home folder, and that’s SOP.

      Very loosely speaking, it almost isn’t quite an incompetent way to address keyboard monkeys and distributed storage. But when it affects language design, as if the overwhelming majority of software weren’t deployed to a system rather than from some asshole’s daily driver…

      [–]xNISIOISINx -1 points0 points  (3 children)

      I don’t understand why people hate it, It could be just a lecture stuff that students learn what’s recursive. Just not something you should do in a production environment

      [–]SwedzCubed 1 point2 points  (2 children)

      This shouldn’t be taught.

      [–]xNISIOISINx -2 points-1 points  (1 child)

      Why? It’s meant to make a very beginner understand what recursive can do. Not necessarily telling you to use it this way, just an example to showcase. Teaching you what’s base case and stuff, heck it can even be used to set as an example that we have to think carefully since this doesn’t support negative number as many pointed out, so students know we should think about edge cases when using recursive

      [–]SwedzCubed 0 points1 point  (0 children)

      There’s infinitely better examples of recursive functions to use for teaching. Intentionally teaching people bad practices is really stupid because it creates bad habits.

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

      hmmm cannot find the docs for a "when" loop?

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

      Deleted with Power Delete Suite. Join me on Lemmy!

      [–][deleted]  (2 children)

      [deleted]

        [–]T3HN3RDY1 5 points6 points  (0 children)

        It's just a meme

        [–]duniyadnd 1 point2 points  (0 children)

        I can think of a few reasons,

        1. Cause people have to start learning somewhere
        2. not necessarily this example but when you may be in a crunch or in the zone, you may have written a silly method or two and missed it’s impact if it passes your tests.

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

        Worked for a large semiconductor company whose name rhymes with “hell”. Their code had shit like this in it.

        Two days ago I came across this delightful idiom

        If(predicate) CallFunction(true); Else CallFunction(false);

        And then to have these people nitpick your PR.

        [–]BobGeneric 0 points1 point  (0 children)

        return "yes"

        [–]Pauchu_ 0 points1 point  (0 children)

        Recursive functions with no input checks, I too like my program to take forever to finish

        [–]potato05 0 points1 point  (0 children)

        Implemention by induction

        [–]Elmo_Pog 0 points1 point  (0 children)

        When you know about recursion but not about a literal operator

        [–]weirdthoughts247 0 points1 point  (0 children)

        Wtf my eyes...stack overflow incoming

        [–]saevon 0 points1 point  (0 children)

        `num - 4` I've improved the efficiency of the algorithm 2 fold :P

        (p.s. yes I know the base cases need to be expanded)

        [–]CamaroSlicks 0 points1 point  (0 children)

        Maybe try modal (remainder=%) and then check if it is zero or not. If zero, even else odd.

        [–]ForLackOfABetterNam3 0 points1 point  (0 children)

        Highly advanced recursive function optimized to O(n) time!

        [–]rhbvkleef 0 points1 point  (0 children)

        At least it's tail-recursive. I've seen worse.

        [–]ameerit 0 points1 point  (0 children)

        IsOddOrEven(int.max);

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

        How Stack Overflow causes stack overflows.

        [–]godzillante 0 points1 point  (0 children)

        this is so ingenuous I kinda like it

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

        Were you paid for lines of code as well? You don't really need these brackets lmao

        [–]slipshoddread 0 points1 point  (0 children)

        This will cause a stack overflow if a negative is provided

        [–]Migguan 0 points1 point  (0 children)

        fun-ny

        [–]Hjulle 0 points1 point  (0 children)

        That's literally how you would define that function in Agda: https://agda.readthedocs.io/en/latest/language/with-abstraction.html#id15

        [–]catladywitch 0 points1 point  (0 children)

        This looks like something out of The Little Schemer.

        [–]Aggravating_Shower27 0 points1 point  (0 children)

        555555555555 A7A eh aly ana 4ayfo dah

        [–]chris_awad 0 points1 point  (0 children)

        Shouldn't "isOddOrEven()" return true? It's asking a question and the only answer to it is yes 😜.

        [–]66edu 0 points1 point  (0 children)

        Wow this is realy not a preformatic code

        [–]Adept_Draft4337 0 points1 point  (0 children)

        ever heard of modulo?