• DumbAceDragon
    link
    fedilink
    English
    101 year ago

    Either engineer or bit hacker, depending on whether or not I’m trying to avoid branching.

  • GTG3000
    link
    fedilink
    Русский
    31 year ago

    Why use const max = (x, y) => x > y ? x : y instead of function max(x, y) { return x > y ? x : y } ?

  • @force@lemmy.world
    link
    fedilink
    English
    21
    edit-2
    1 year ago

    wtf kind of cursed programming language is this? JS? it’s so ugly, in no universe should a function look like that

    but obviously as a rust enjoyer i have to do it like

    fn max ⟨T: PartialOrd + Copy⟩(nums: ⁊[T]) -> Option⟨T⟩ {
        let mut greatest: ⁊T = ⁊nums[0];
        match nums.len() {
            0 => None,
            1 => Some(*greatest),
            _ => {
                for num in nums {
                    if num > greatest {
                        greatest = num;
                    }
                }
                Some(*greatest)
            }
        }
    }
    

    edit: lemmy formatting REALLY hates references and generics it seems… time to go back to medieval times

    • @stingpie@lemmy.world
      link
      fedilink
      41 year ago

      Ah yes, rust. The language that somehow manages to manages to as verbose as possible, with as much jargonized shorthand that a computer could handle.

      • @aidan@lemmy.world
        link
        fedilink
        21 year ago

        Exactly, I don’t understand why languages have decided that every keyword needs to be as randomly minified as possible. fn, def, rune(ok that’s not minified, just a dumb name), fmt, std. Many of these things aren’t new, but programmers recognize descriptive variable names are important, the same should be true for keywords.

    • @Cihta@lemmy.world
      link
      fedilink
      41 year ago

      Wow that’s a very exhausting language. I dropped your code into an online rust to asm converter and it actually wasn’t more! I did try to post it for fun but lemmy kept messing up the code block. Oh well, wasn’t that amusing anyway!

      • @force@lemmy.world
        link
        fedilink
        41 year ago

        lol that’s not actually how rust is written, it was just a joke

        it’d really be written

        if x > y { x } else { y }
        
        • @Cihta@lemmy.world
          link
          fedilink
          11 year ago

          Hah thanks for clarifying. I was joking too and it’s a shame I couldn’t post the results.

          Though I admit i don’t know anything about rust. I’m sure I’d like it better than the proprietary garbage i use now that just gets converted to ASM / PLC code in the end. But I can’t skip the middle man. I’m not gonna try but probably 30mins for me to “write” the above.

          Besides, how do you make money if I can code something in an hour as opposed to 2 days?

            • @Cihta@lemmy.world
              link
              fedilink
              31 year ago

              No no no you misunderstood me.

              I was being honest, I know nothing of rust. I have however used python in embedded systems with positive results. The product didn’t make it but for other reasons.

              Funny you mention java, that’s sorta what I’m stuck in but not like you think. Beyond the fact that it’s a bloated nightmare.

              I’m just a low-level programmer at heart but I have bills to pay. The rust stuff was all just a joke… i don’t know it but maybe i should. Thanks for the info.

              Saying anymore about what I do is just super embarrassing but i promise i meant no ill will. Excuse my frustration, I’m locked into a proprietary system i have no control over. You would laugh your ass off if you saw it. Anyway, i meant no offense, have a good night!

  • lwhjp
    link
    fedilink
    761 year ago

    TDD

    const max12 = (x, y) => {
        if (x === 1 && y === 2) {
            return 2;
        } else if (x === 7 && y === 4) {
            return 7;
        } else {
            return x;
        }
    };
    
  • @kewko@lemdro.id
    link
    fedilink
    English
    301 year ago

    Mathematician 2 kinda blew my mind, kinda obvious, just can’t believe I was never taught or thought about it.

      • @uberrice@feddit.de
        link
        fedilink
        31 year ago

        Simple, really. Abs(x-y) is the difference between the two numbers, absolute, so positive value. So, adding abs(x-y) to the smaller of the two numbers turns it into the bigger number. Plus the bigger number, now you have 2 times the bigger number

    • @Tolookah@discuss.tchncs.de
      link
      fedilink
      41 year ago

      Engineer likely ends up with the smallest code. Though the hit to execution time for a branch sucks. (Pipelines and such)

      Bit hacker will take the least execution time because of pipelines, but it needs more comments. Maybe something like // trust me, this works.

    • Zagorath
      link
      fedilink
      English
      16
      edit-2
      1 year ago

      Well, the question sort of implies that you’re needing to implementing Math.max yourself, for whatever reason. Probably as an exercise. It doesn’t make sense to reuse a library that implements the feature if you’re explicitly being asked how you would implement it yourself.

      • Demonen
        link
        fedilink
        English
        131 year ago

        This is why I think school and interviews are like a whole different universe from the one where actual work gets done.

        • Zagorath
          link
          fedilink
          English
          81 year ago

          In some ways they can be wholly different, but I don’t think this is a good example of it.

          Any programmer who cannot implement “take two numbers, return the larger one” is clearly not very competent. Even though you’re never going to literally need to implement Math.max yourself at work, you are going to need basically the same types of skills. Probably 95% of the work I do day-to-day is stuff you’d learn in your first year at uni, and this just shows that you’ve got that ability.

          In practice, the best interviews I’ve had usually set a slightly more complicated task as a do-in-your-own-time problem and then go through what you did in the actual interview. Problems like “read a list of names in the form , each name on a separate line, from a text file. Sort the names by last name, then by other names. Output to another text file. Include unit tests.” They wouldn’t then expect you to re-implement the sorting algorithm itself, but more want to look at the quality of code, extensibility, etc.

          More basic questions like the one in the OP, or fizzbuzz, are decent as well, and a big step up from lame questions like “what does SOLID stand for? What does the Liskov substitution principle mean to you?” Even if they’re not quite as valuable as a miniature project.

          • @Rodeo@lemmy.ca
            link
            fedilink
            21 year ago

            In practice, the best interviews I’ve had usually set a slightly more complicated task as a do-in-your-own-time problem and then go through what you did in the actual interview.

            The best interviews you’ve had are the ones where you’re doing free work on your own time?

            • Zagorath
              link
              fedilink
              English
              21 year ago

              “Work” is a debatable term. It’s not work that provides any direct value to the company, if that’s what you mean. But yes, it involves more effort on my part.

              But yes. Not only does this method let me show that I’m good at what I do (far better than nonsense theory questions do), I have also found that companies that use this approach tend to come across as a better fit in other ways during the interview process.

          • @hatchet@sh.itjust.works
            link
            fedilink
            21 year ago

            I think you can probably make the question a lot more interesting by asking them to implement max without using any branching syntax. I’m not saying that is necessarily a good interview question, but it is certainly more interesting. That might also be where some of the more esoteric answers are coming from.

          • Demonen
            link
            fedilink
            English
            11 year ago

            For me, a good interview is a dialogue where the company representative shows me as much about the company as I do about me as a candidate. Take-home tasks are okay, I guess, but I suspect they might balk at me requesting they handle a mock HR issue, or whatever, for me!

    • @MajorHavoc@lemmy.world
      link
      fedilink
      41 year ago

      Why would you use anything other than Math.max?

      I mean, I might be being paid by the hour or my performance measured by lines of code…