Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe operation to function left-to-right #129

Closed
DiermenSoftware opened this issue Dec 20, 2024 · 5 comments
Closed

Pipe operation to function left-to-right #129

DiermenSoftware opened this issue Dec 20, 2024 · 5 comments

Comments

@DiermenSoftware
Copy link

DiermenSoftware commented Dec 20, 2024

Currently this is the result of the pipe operator:

function = (str1,str2,str3) => str1 + " " + str2 + " " + str3;
function("1","2","3"); //returns: "1 2 3"
"1"|function("2","3"); //returns: "2 3 1"  desired: "1 2 3"

Would it be possible to optionally change this behaviour in the library settings so that the left statement is injected into the first parameter of a function?

@FlorianRappl
Copy link
Owner

Hm that is independent of the Pipe operator. I think what you are looking for is changing the behavior of currying in MAGES.

Currying states that f(x,y,z) = f(x)(y)(z), such that g(z)=f(x, y).

(See: https://github.com/FlorianRappl/Mages/blob/devel/doc/language.md#auto-currying)

What we could think about is to introduce a syntax such as _ to denote where missing arguments should be inserted:

"1" | function(_, "2", "3") // returns "1 2 3"
"2" | function("1", _, "3") // returns "1 2 3"
function("1", _, "3") // returns a new function with one parameter, aliasing g(x) = function("1", x, "3")

Such a language extension could be useful on other locations, too. What are your thoughts?

@DiermenSoftware
Copy link
Author

DiermenSoftware commented Dec 21, 2024

This would be a nice option, however the built-in functions still work in the same way.
The currying is indeed the way to make it work more like C# logic for example:

1.123|round(1);  // should be 1.1
round(1.123, 1); // should be 1.1
10|sub(2); // should be 10-2
sub(10, 2); // should be 10-2

Than probably the order of those functions need to be swapped.

@FlorianRappl
Copy link
Owner

FlorianRappl commented Dec 21, 2024

Yeah we don't want to change the currying and for technical reasons you cannot go from left-sided to right-sided (as a matter of fact for most / some functions we don't know "when" the upper bound of arguments ends, i.e., if you have f(x, y, z) but f already returns something useful for f(x, y) then you cannot eat arguments from the right).

So the only reliable way is from the left.

I actually think that the _ (leave the argument open) operator is what we need here. It would solve

2 | sub(_, 10) // would be sub(2, 10) = 10-2
2 | sub(10) // would be sub(10, 2) = 2-10

quite elegantly imho.

@FlorianRappl
Copy link
Owner

Pushed an implementation of the placeholder parameter - available in the preview version.

@DiermenSoftware
Copy link
Author

You are right, this is indeed a very elegant solution that also makes the piping more flexible.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants