• bss03@infosec.pub
    link
    fedilink
    English
    arrow-up
    9
    ·
    edit-2
    6 months ago

    This is my favorite language: GHC Haskell

    GHC Haskell:

    GHCi> length (2, "foo")
    1
    
    • yetAnotherUser@lemmy.ca
      link
      fedilink
      arrow-up
      6
      ·
      6 months ago

      Wait, now I need to know why.

      * some time later *

      I went to check why the hell this happened. It looks like the pair (“(,)”) is defined as an instance of Foldable, for some reason, which is the class used by functions like foldl() and foldr(). Meanwhile, triples and other tuples of higher order (such as triples, quadruples, …) are not instances of Foldable.

      The weirdest part is that, if you try to use a pair as a Foldable, you only get the second value, for some reason… Here is an example.

      ghci> foldl (\acc x -> x:acc) [] (1,2)
      
      [2]
      

      This makes it so that the returned length is 1.

      • bss03@infosec.pub
        link
        fedilink
        English
        arrow-up
        6
        ·
        6 months ago

        Oddly enough, in Haskell (as defined by the report), length is monomorphic, so it just doesn’t work on tuples (type error).

        Due to the way kinds (types of types) work in Haskell, Foldable instances can only operate over (i.e. length only counts) elements of the last/final type argument. So, for (,) it only counts the second part, which is always there exactly once. If you provided a Foldable for (,) it would also have length of 1.

        • bss03@infosec.pub
          link
          fedilink
          English
          arrow-up
          2
          ·
          6 months ago

          (.) is a valid expression in Haskell. Normally it is the prefix form of the infix operator . that does function composition. (.) (2*) (1+) 3 = ((2*) . (1+)) 3 = 2 * (1 + 3) = 8.

          But, the most common use of the word “boob” in my experience in Haskell is the “boobs operator”: (.)(.). It’s usage in Haskell is limited (tho valid), but it’s appearance in racy ASCII art predates even the first versions on Haskell.

        • Dr. Moose@lemmy.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          edit-2
          6 months ago

          It looks like two worms split running from another tinier worm. Makes you wonder what it has done to be so feared

  • arc@lemm.ee
    link
    fedilink
    arrow-up
    13
    arrow-down
    1
    ·
    edit-2
    6 months ago

    Javascript is a dogshit language that everyone is stuck with. The best that we can hope for is the likes of typescript take the edge off of it. Even though it’s like smearing marzipan over a turd. At least it’s ok if you don’t take a deep bite.

    • Fijxu@programming.dev
      link
      fedilink
      arrow-up
      6
      ·
      6 months ago

      JS should have never leaved the Browser side. Now you can use this thing for Backend and is just awful

  • wreleven@lemmy.ca
    link
    fedilink
    English
    arrow-up
    13
    ·
    6 months ago

    Feels like it could be one of those facebook posts to test “smart” people. Only the top 1% of people can answer this simple math question: “11” + 2 * 2 - 3

  • proctor1432@lemmy.world
    link
    fedilink
    arrow-up
    12
    ·
    6 months ago

    Heck, I need to learn some new languages apparently. Here I was expecting an angry "CS0029 cannot implicitly convert type ‘string’ to ‘int’!

    • Alexstarfire@lemmy.world
      link
      fedilink
      arrow-up
      24
      arrow-down
      4
      ·
      6 months ago

      Unfortunately, it makes sense if you know what + means, which is concatenate. - is strictly a math function though.

      Not saying that makes this better. It just makes sense.

      • grue@lemmy.world
        link
        fedilink
        arrow-up
        28
        arrow-down
        3
        ·
        edit-2
        6 months ago

        It is ‘comprehensible’ in the sense that it’s possible to figure out how it happened, but it absolutely does not “make sense” in terms of being a reasonable language design decision. It’s 100% incompetence on the part of the person who created Javascript.

        • Fushuan [he/him]@lemm.ee
          link
          fedilink
          arrow-up
          2
          ·
          6 months ago

          It makes perfect sense if the Lang objective is to fail as little as possible. It picks the left side object, checks if the operand is a valid operand of the type. If it is, it casts the right variable into that type and perform the operand. If it isn’t, it reverses operand positions and tries again.

          The issue here is more the fact that + is used both as addition and as concatenation with different data types. Well, not an issue, just some people will complain.

          • grue@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            6 months ago

            Computing a nonsensical result is itself a failure. Continuing to run while avoiding giving an error in that case accomplishes nothing but to make the program harder to debug.

        • Alexstarfire@lemmy.world
          link
          fedilink
          arrow-up
          7
          ·
          6 months ago

          I mean, I’d never try to do this anyway because if the types aren’t the same unexpected things can happen. That’s like programming 101.

        • palordrolap@fedia.io
          link
          fedilink
          arrow-up
          4
          ·
          6 months ago

          Perl is an old but notable exception. + is purely for addition in the base language.

          If you try to add two strings with it, they’ll be converted to numbers based on any number-like characters they have at their left hand ends, and, if warnings are enabled (and you should definitely do that), you’ll get runtime warnings about it if there’s even anything vaguely non-numeric about them.

          e.g. “1”+“11” will get you 12 with no complaint, warnings or otherwise. Not even the string “12” either, although it’s hard to determine one from the other in Perl. It’s a need-to-know kind of thing. And you generally don’t.

          “a”+“bb” gives 0 as the result because they’re not numbers and “1a”+“11bb” will give 12, but these latter two will give warnings. Two each, in fact, one for each dodgy parameter.

          String concatenation is done with the dot operator instead. “1”.“11” gives “111”. This comes with it’s own minor problems, but at least + is safe.

          • nyan@lemmy.cafe
            link
            fedilink
            English
            arrow-up
            3
            ·
            6 months ago

            That’s because Perl doesn’t do operator overloading in general. Even the equality operator is different for strings (eq instead of ==). As a language, it may look pretty weird and lack some modern features, but the underlying design is surprisingly intelligent and consistent in many ways.

            • palordrolap@fedia.io
              link
              fedilink
              arrow-up
              1
              ·
              6 months ago

              Not strictly true.

              Perl’s default bitwise operators do differentiate between numbers and strings that look like numbers in a way that addition doesn’t*, and the readline/glob operator <> does different things depending on what (if anything) is between the signs.

              There’s also the whole overload pragma for objects, which doesn’t affect default data types, but if you’re sufficiently perverse, you can define a String class that uses ‘+’ like JavaScript.

              * in 2015, they added new operators so that those and the original operators don’t overload and have only one specific purpose if the bitwise pragma Edit: feature is turned on. You might know all this already though.

    • Gsus4@mander.xyz
      link
      fedilink
      arrow-up
      110
      ·
      edit-2
      6 months ago

      Hm, playing devil’s advocate, I think it is because the minus has not been defined as a string operation (e.g. it could pop the last char), so it defaults to the mathematical operation and converts both inputs into ints.

      The first is assumed to be a concat because one of the parcels is a string…

      It’s just doing a lot of stuff for you that it shouldn’t be in first place 🤭

      • dalekcaan@lemm.ee
        link
        fedilink
        arrow-up
        13
        arrow-down
        1
        ·
        6 months ago

        Yeah, this looks dumb on the surface, but you’ve got bigger problems if you’re trying to do math with strings

        • Gsus4@mander.xyz
          link
          fedilink
          arrow-up
          18
          ·
          edit-2
          6 months ago

          Yeah, I actually had to try 1+“11” to check that it didn’t give me 12, but thankfully it commutes it’s consistent 😇

          • palordrolap@fedia.io
            link
            fedilink
            arrow-up
            17
            ·
            6 months ago

            it commutes

            Maybe the behaviour with regard to type conversion, but not for the operation itself.

            “13”+12 and 12+“13” don’t yield the same result.

              • palordrolap@fedia.io
                link
                fedilink
                arrow-up
                4
                ·
                6 months ago

                Given it’s JavaScript, which was expressly designed to carry on regardless, I could see an argument for it returning NaN, (or silently doing what Perl does, like I mention in a different comment) but then there’d have to be an entirely different way of concatenating strings.

                • ChickenLadyLovesLife@lemmy.world
                  link
                  fedilink
                  English
                  arrow-up
                  5
                  ·
                  6 months ago

                  expressly designed to carry on regardless

                  I’m surprised they didn’t borrow On Error Resume Next from Visual Basic. Which was wrongly considered to be the worst thing in Visual Basic - when the real worst thing was On Error Resume. On Error Resume Next at least moved on to the next line of code when an error occurred; On Error Resume just executed the error-generating line again … and again … and again … and again …

      • 0x0@lemmy.zip
        link
        fedilink
        arrow-up
        12
        ·
        6 months ago

        It’s just doing a lot of stuff for you that it shouldn’t be in first place 🤭

        Kinda like log4j!

  • kubica@fedia.io
    link
    fedilink
    arrow-up
    23
    ·
    6 months ago

    Lets fix it. I think that since we are removing the ones, then “11” - 1 should be equal to “”.

      • grue@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        6 months ago

        Which “1” did it remove? And did it search the string to find a “1” to remove, or did it remove whichever character happened to be at array index 1?

    • Shanmugha@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      6 months ago

      Hear me out:

      “11” - 1 = “11” - (-1) = “11” (did not find “-1” in "11)

      Or

      “11” - 1 = “11” - (-1) = “1” (removed first “1”)

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

      This is a really good interview, and does a good job highlighting Javascript’s biggest strength: it’s flexibility.

      “It was also an incredible rush job, so there were mistakes in it. Something that I think is important about it is that I knew there would be mistakes, and there would be gaps, so I made it very malleable as a language.”

      He cites the “discovery” of asm.js inside of JavaScript, calling it “another thing I’m particularly proud of in the last 10 years.” It uses the bitwise operators that were included in the original JavaScript which are now the basis for a statically-typed language with machine types for high-speed performance. “If it hadn’t been in there from 1995, it would’ve been hard to add later. And the fact that it was there all along meant we could do incredibly fast JavaScript.”

      He tells InfoWorld it’s “this very potent seed that was in the original JavaScript from the 10 days of May in 1995.” JavaScript’s 32-bit math operators (known as bitwise operators) trace their lineage all the way back to the C programming language — and to Java. This eventually led to WebAssembly — a way to convert instructions into a quickly-executable binary format for virtual machines — and the realization that with a JavaScript engine, “you can have two languages — the old language I did with the curly braces and the functions and the shift operators, and this new language which is a binary language, not meant for reading by humans or writing. But it can be generated by compilers and tools, and can be read by tools…”

  • REDACTED@infosec.pub
    link
    fedilink
    English
    arrow-up
    33
    arrow-down
    11
    ·
    6 months ago

    Imagine doing math with strings and then blaming the language not yourself

    • zagaberoo@sopuli.xyz
      link
      fedilink
      arrow-up
      41
      ·
      6 months ago

      The risk is when it happens unintentionally. The language is bad for hiding such errors by being overly ‘helpful’ in assuming intent.

      • FooBarrington@lemmy.world
        link
        fedilink
        arrow-up
        9
        arrow-down
        6
        ·
        6 months ago

        Sure, but at this point it’s your own fault if you don’t use Typescript to keep these issues from happening.

          • matlag@sh.itjust.works
            link
            fedilink
            English
            arrow-up
            7
            ·
            6 months ago

            That’s also my understanding: “Javascript is great because you can use other languages and then transpile them to JS.”

            • FooBarrington@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              edit-2
              6 months ago

              JS itself is great, I prefer it to most other languages due to the flexibility that it allows. Adding types through TS to safeguard against footguns doesn’t mean you’re not still using JS. You can also add the types using comments instead if you prefer it, which means you’re actually writing raw JS.

          • sebsch@discuss.tchncs.de
            link
            fedilink
            arrow-up
            1
            ·
            6 months ago

            Yeah! Wasm is a thing. At least rust and go are pretty neat in the browser lately.

            We should leave that pile of semantics and just go further with web development

        • jjjalljs@ttrpg.network
          link
          fedilink
          arrow-up
          22
          arrow-down
          1
          ·
          6 months ago

          “Use a different language” is a common defense of javascript, but kind of a weird one.

          • FooBarrington@lemmy.world
            link
            fedilink
            arrow-up
            5
            ·
            6 months ago

            Not really, considering Typescript only adds static types to JS. It’s not a different language, it’s an extension.

            • Quibblekrust@thelemmy.club
              link
              fedilink
              English
              arrow-up
              7
              ·
              6 months ago

              Since it needs to be compiled to JavaScript in order to be used, I kind of consider it a different language. Yes, it’s a strict superset of JavaScript, but that makes it different.

              • FooBarrington@lemmy.world
                link
                fedilink
                arrow-up
                2
                ·
                edit-2
                6 months ago

                That’s your prerogative, but it honestly doesn’t make sense. Typescript adds almost no functionality to JS (and the few pieces it adds are now considered mistakes that shouldn’t be used anymore). It only focuses on adding typing information, and in the future you’ll be able to run TS that doesn’t use those few added features as JS (see the proposal).

                You can also add the TS types as comments in your JS code, which IMO shows that it’s not a different language.

  • kamen@lemmy.world
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    6 months ago

    If you’re consciously and intentionally using JavaScript like that, I don’t want to be friends with you.