1. 55
  1.  

  2. 8

    I feel like I should hate it, but I don’t. This is so cool.

    1. 7

      This is beautiful, I love it.

      1. 3

        Additionally one prefix operator for negation would be nice. Since - is already in use, I chose ~.

        It seems like it might be possible to treat - as either a binary or a unary operator depending on the context: if it has whitespace before it but not after it—which is illegal for other operators—it’s the unary operator, and otherwise it’s the binary operator. That would make it possible to write things like 3 + -2. (One downside is that the mandatory whitespace on the left would prevent you from writing things like 20+-10 / 2, which would require parentheses if you were going to insist on negating the 10.)

        1. 2

          Haskell does something similar: if - has nothing on its right-hand side, it’s a prefix negation operator; otherwise it’s subtraction. This rule is an exception to how the rest of the language works, but it’s considered an acceptable compromise.

          Another thing I considered was making + and - part of the number literal, so that -1 can be parsed as a single token in addition to - being an infix operator. This makes parsing numbers a bit more complicated, but 1 -2 is still invalid, so it seems to work. The practical effect of this is similar to your idea, and I believe Fortran does this.

          I didn’t agonize over the decision though. This calculator is mostly a proof-of-concept for an idea that I wanted to try out, and the simplest thing was to pick a different symbol for unary negate.

          It’s worth noting too that the concept of -1 can be understood as beginning with an implicit 0, as in 0-1. This makes a negation operator pretty unnecessary anyway!

        2. 3

          dc is the standard calculator

          1. 2

            Nim used to do this experimentally with a “strong spaces” setting in 2014, but I guess it was not popular enough or viewed as too error prone { or unpopular due to that view :-) } and got removed in 2019.

            1. 2

              I actually used this idea in a Lisp-based language :)

              http://akkartik.name/post/wart

              1. 2

                @mlochbaum’s I does this as well.

                1. 2

                  I find this unsettling. The world is tipping sideways. Who are you people? ;-)

                  1. 2

                    Cool!

                    I personally think that a lot of the annoyances associated with parentheses would be eliminated if the calculator just had a “wrap last thing(s) in parentheses” key binding. For example, with | representing the cursor:

                    6+5+4+3| [press C-b C-b]
                    6+(5+4+3)|
                    
                    1. 1

                      That’s a neat idea! Although in your example, why wouldn’t the result be (6+5+4+3)? How does it choose where to insert the paren?

                      Maybe typing an unmatched ) could wrap the entire input so far in parens. Like when people say “the quantity squared” or “all over 2a”.

                      6+5+4+3|  [press )^2]
                      (6+5+4+3)^2|
                      

                      Or, maybe repeating ) would grab larger and larger subexpressions

                      6+5+4+3| [press )]
                      6+5+(4+3)| [press )]
                      6+(5+4+3)| [press )]
                      (6+5+4+3)|
                      
                      1. 2

                        Although in your example, why wouldn’t the result be (6+5+4+3)?

                        I was thinking that one Ctrl-b wrapped the previous operation in parentheses and continued consuming more when pressed again.

                        Or, maybe repeating ) would grab larger and larger subexpressions

                        Wow, that’s such a good idea! And perhaps Esc/Alt-) could “unconsume” the first thing in the parentheses. That would make a great interactive calculator, not too hard to implement for a terminal emulator either.

                    2. 2

                      If anyone else finds themselves looking for a decent calculator program - I recommend giving frink a try.

                      It’s a units-aware calculator - that is, if you multiply 3 meters / 5 seconds you get 15 meters per second.

                      In my experience using it, this wipes out a substantial class of bugs - if the final result is not in the expected units (whether that be US dollars or kg/m^2), you know you’ve made an error.

                      1. 1

                        Frink looks awesome, definitely going to give it a spin :)

                      2. 1

                        wcalc is a great, simple, text-mode calculator. its been in the ubuntu package archive since at least 16.04.

                        1. 1

                          Not to be discouraging or anything, but that looks absolutely horrible. It might be slightly easier to write, but looks terrible to read, plus it invents it’s own, new, never used anywhere, convention for something that already has a convention pretty much set in freaking stone (parenthesis).

                          I mean, scratch your itch, by all means, but in terms of language syntax design that just looks like a bad idea all around.

                          1. 2

                            Not trying to judge, but every convention was once new.

                            1. 1

                              Fair enough, but being new is not my only criticism, it’s more like the straw that breaks the camel’s back, really.

                              1. 1

                                Agreed :)

                          2. 1

                            It would be really cool if someone would modify the CPython interpreter to do this! (Any other language would be fine, too.)

                            1. 1

                              The typical tokenizer/AST pipeline throws away the visual grouping information contained in whitespace, much to nobody’s benefit. I hope for the exploration of fixed-format languages. Does it treat doubled spaces as an error? That’s absolutely the sort of typo I make.

                              1. 1

                                I can get behind cases where spacing and precedence match. I’d love if automatic code formatters and linters to followed that style, so that if you write w+x / y+z, you’ll see the error when it shows up as w + x/y + z.

                                1. 1

                                  I believe gofmt does this!