[feature request] pipeline operator

Issue #726 new
Mihai Ionescu created an issue

Was is ever planned? Something like:

1 + 2 |> _ + 6 |> sqrt(_)

Another way to see it would be with multiple lines. I think this would be they smoother way with speedcrunch style (semicolon instead of comma for parameters)

1 + 2 ; _ + 6; sqrt(_)

Comments (5)

  1. Helder Correia repo owner

    I'd like to see some more use cases to understand the usefulness of the introduction of result pipelining in SpeedCrunch. The one you presented doesn't justify the implementation effort, I'm afraid. You could just as well have written sqrt(1+2+6)

  2. Mihai Ionescu reporter

    From my perspective at least, the biggest gain is alignment to how brain works, when solving stuff, in consecutive steps, modifying the result, like a fractal. It's very intuitive.

    I don't know about others, but i use the calculator for trial and error stuffs, ideas, etc. I don't know from the beginning all the steps, all the details, all the flow.

    Pipeline operator is very advantageous for this, like, when you do

    sqrt(5)
    

    then you want to add another function to it, like int(), say:

    int(sqrt(54))
    

    you have to go back at the beginning, write int(, then go back at the end, close the parentheses

    Then you want to add another function, do it all over again, and it looks ugly, maybe good for the computer, but from humans looks ugly.

    Yes, i could use a single function per line, and use ans again and again. But that's exactly what pipeline operator would do, in a much smaller space, in a more intuitive way, single line.

    Usually i don't want to overpopulate the space with intermediary steps of an idea. And usually, all my ideas or needs involve from 2 to 5 steps or functions. So having the possibility to do all 2-5 steps on one line, per each idea, would be very nice and useful.

    I didn't think too much about the effort of implementation. I thought it's just about splitting at semicolons, and making an alternative to ans, the underscore. Then purely replacing the underscores with intermediary calculated answers. Calculate, replace next underscore, calculate, replace next underscore, etc.

    I think it would help if more users would give their opinion about this. From my perspective, after i used the pipeline operator, a little, in F# and julia, i was hooked immediately. Having this in calculators, would rock.

  3. Pol Welter

    I have mixed feelings about a line separator (semicolon, colon or whatever). There is no real benefit here, when compared to actually switching to a new line. Note that when pasting multiline input into SC, it is already split correctly.

    No offense, but I don't like the syntax suggested by @imflorin. It basically just replaces ans by _. I find it hard to read, and it is barely a single keystroke quicker.

    Instead I suggest taking an option similar to what Mathematica. A new operator (// in Mathematica, we may also call it @ or | or whatever), such that

    expr1 @ expr2 = expr2(expr1)
    

    This way one could write

    ans @ sqrt
    

    or

    ans @ hex
    

    Especially the latter might be useful, if you want to quickly reformat the last result.

    The operator needs to be left-associative:

    expr1 @ expr2 @ expr3 = (expr1 @ expr2) @ expr3
    

    and have low precedence (lower than addition).

  4. Mihai Ionescu reporter

    Pol, you are not quite fair here.

    Yes, your proposed style is more readable (if instead of @ we would use semicolons)

    But you thought just about single parameter functions. I was thinking about many parameter functions.

    It would be best to include both ways. So there will be no parentheses with single parameter functions.

    And the remark "barely a single keystroke quicker". Again, not fair. Underscore is 3 times faster than ans. And 3 times smaller. And way more readable for the brain. Your brain doesnt need to interpret the word ans.

    If you use the calculator rarely, then this discussion doesnt matter. Again, not fair as i see it. Because i was talking about high volume of calculations and steps.

    I would rather have underscore and semicolons. Than 100 lines, for 20- 25 cycles. (Cycle = one full idea from start to end)

  5. Pol Welter

    No hard feelings :)

    I am actually very grateful for any discussion. Still, I fear I might not get your point though.

    AFAICT, you are suggesting for two things.

    1. _ as an alias for ans. I personally am in the explicit-is-better-than-implicit camp, and I largely prefer ans for better readability. My brain doesn't seem to work like yours :) I can see where you are coming from though. Also, adding this feature would have no real downsides for existing users.

    2. An expression separation operator, namely ;. It is this one I am rather opposed to. First, the semicolon is the argument separation operator, so it is out of question we use it for a completely different purpose as well. Secondly, I think it leads to very bad readability. In pretty much every programming language, you are strongly discouraged from putting more than one statement per line. Things like 1 + 2 ; _ + 6; sqrt(_) look atrocious in my book.

    I would rather have underscore and semicolons. Than 100 lines, for 20- 25 cycles. (Cycle = one full idea from start to end)

    Why would 100 lines be a bad thing? Are you concerned about losing track of your work? Are you often repeating similar calculations?

  6. Log in to comment