• Laser@feddit.org
    link
    fedilink
    arrow-up
    16
    ·
    7 hours ago

    Not sure I’d call what bash has functions. They’re closer to subroutines in Basic than functions in other languages, as in you can’t return a value from them (they can only return their exit code, and you can capture their stdout and stderr). But even then, they are full subshells. It’s one of the reasons I don’t really like Bash, you’re forced into globally or at least broadly-scoped variables. Oh, and I have no clue right now how to find where in your pipe you got a non-null exit code.

    It’s not a big problem for simple scripting, but it makes things cumbersome once you try to do more.

    • SlurpingPus@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      21 minutes ago

      Functions are definitely not subshells in Bash, seeing as anything modifying the environment, like pyenv and such, is implemented as functions instead of scripts — specifically because functions are run in the same shell instance.

      Unless ‘subshell’ means something in the vein of ‘like a new shell, but not really’.

    • yetAnotherUser@discuss.tchncs.de
      link
      fedilink
      arrow-up
      2
      ·
      3 hours ago

      I really like bash when dealing with even somewhat advanced scripting. Like the 300 LOC scraper I have written over the past two days which horribly parses HTML files using grep | sed.

      It’s genuinely so much more fun to do this with Bash than, say, Python. I have once written a scraper using Beautifulsoup and I have no desire to do so ever again.

      Honestly, only Haskell manages to beat Bash in how satisfying it feels when you manage to get something working well.

    • SlurpingPus@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      11 minutes ago

      Remarkable how if the parenthesis is shifted from lambda() to (lambda), people lose the ability to comprehend things.

  • NotSteve_@piefed.ca
    link
    fedilink
    English
    arrow-up
    27
    ·
    edit-2
    8 hours ago

    def (): is pretty nice

    Edit: also as someone doing a bunch of CI work right now, Bash can GTFO (unless the alternative is whatever Windows is doing)

  • Speiser0@feddit.org
    link
    fedilink
    arrow-up
    9
    ·
    10 hours ago

    C++ has []{}.

    (You can also add more brackets if you wish to do nothing longer: []<>[[]]()[[]]{}())

    • spongebue@lemmy.world
      link
      fedilink
      arrow-up
      4
      ·
      11 hours ago

      I have no idea why you’d need that especially since return y() is pretty easy, but… I want it!

      (Actually, I guess a super simple way of overloading a method, like fun x() = x(defaultValue) could be neat)

      • calcopiritus@lemmy.world
        link
        fedilink
        arrow-up
        6
        ·
        10 hours ago

        This can also be a side product for code blocks being expressions instead of statements.

        In rust for example they are, so it’s not rare to see functions like:

        fn add_one(x: i32) -> i32 {
            x+1
        }
        

        This lets you do amazing things like:

        let x = if y < 0.0 {
            0.0
        } else {
            y
        }
        

        which is the same as x = y < 0.0 ? 0.0 : y

        But is much better for more complex logic. So you can forget about chaining 3-4 ternary operations in a single line.

        • SlurpingPus@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          51 minutes ago

          Lisp programmers seeing these ‘amazing things’:

          But yeah, every time I’m trying to do a ternary in Lua, I miss being able to just throw in an if.

      • Eager Eagle@lemmy.world
        link
        fedilink
        English
        arrow-up
        3
        ·
        edit-2
        9 hours ago

        default values is one of my pet-peeves after using Python regularly. I wish more languages would let you just do something like def do_thing(arg=default_value) without hoops like builder pattern, function overloading, or whatnot

        • SlurpingPus@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          26 minutes ago

          I mean, the go-to approach in Lisp, for example, is to have null as the default value (which doubles for false in there). And check for that in the function.

            • SlurpingPus@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              1 minute ago

              In Lisp, at least the Emacs Lisp with which I have experience, it’s customary to put in nil (Lisp’s null) for any omitted arguments in the middle that you can’t be arsed to specify — aside from just leaving off arguments at the end. In JS, typing in undefined in every such case would probably be an annoyance, so I’m guessing coders need to check for both undefined and null in these circumstances.

              Overall, it’s remarkable how Lisp teaches one to be much more relaxed about programming practices than is typical for mainstream languages. Design patterns? Data structures? Shit, just pass in a list or an assoc array, and maybe a function here and there. Also everything is an expression, enjoy your ternary (if) at any point anywhere.

    • masterspace@lemmy.ca
      link
      fedilink
      English
      arrow-up
      10
      ·
      edit-2
      11 hours ago

      The equivalent in JavaScript / TypeScript would actually be function () {}, this is the syntax for named functions.

      C# is the same as bash though.

          • SpaceCowboy@lemmy.ca
            link
            fedilink
            arrow-up
            1
            ·
            10 hours ago

            Yeah for whatever reason, FE devs want to make everything a const. It’s like a religious belief or something, it’s really kinda weird.

            const fun = () => { const something = “whatever” const array = []; array.push(someting)

            for (const thing of array) { if (thing === ‘whatever’) blah(thing) } }

            Semicolons? Optional. Which quotes you should use? Whatever you feel like! But you must declare things as a const wherever possible! Even if it’s an array that you’re going to be changing, declare it as a const because you should know that you can push things into a const array, and since it’s possible to declare it as a const, you must declare it as a const.

            Why is this? Nobody knows, but it’s important to FE devs that you use const.

            • Ghoelian@piefed.social
              link
              fedilink
              English
              arrow-up
              8
              ·
              9 hours ago

              The reason is very simple, performance. If a value doesn’t need to be changed, don’t declare it as mutable. This isn’t just a front-end thing btw.

              • SpaceCowboy@lemmy.ca
                link
                fedilink
                arrow-up
                1
                ·
                8 hours ago

                Pushing something onto an array isn’t changing the array? It’s not changing the reference to the array, but from a style standpoint it doesn’t make sense.

                And if you’re declaring a const within the scope of a function, it’s still allocating memory when it enters the scope and disposing it when it leaves the scope, same as a variable. There’s no performance benefit to do this.

                Something like const CONSTANT_VALUE = “This never changes” has a performance benefit and is actually how other languages use constants. The value will always be the same, the compiler understands this and can optimize accordingly. If you’re declaring an iterator or the result of calling a webservice to be const it’ll be a different value every time it runs that code, so it’s not something a compiler can optimize. In style terms, it’s a value that’s different every time you get to that line of code, so why would you want to call it constant?

                You’re comment indicates the FE dev obsession with always using const stems from a misunderstanding of how computers work. But of course many religious beliefs originate from a misunderstanding of the world. Whatever man, I just make it a const to make the linter happy, because it’s dumb FE bullshit LOL.