You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SUGGESTION] Implement the pipeline operator from P2011 & P2672 #741
Suggestion
Cpp2 could support the pipeline operator |> as proposed in P2011 and further explored in P2672.
Specifically, the pipeline operator with the "placeholder model" with mandatory placeholder (e.g. $), as described in P2672 section 6 Disposition. Both papers explain the problem and motivation for the new operator, as well as discussing options for the placeholder token.
Circle uses $ as its token.
The proposed operator enables a simpler left-to-right style as opposed to an inside-out style.
Conor Hoekstra (code_report) has various talks about ranges and pipelines and explains how the pipeline operator can make the code simpler and more readable. The following is one of his examples:
Without the operator:
auto filter_out_html_tags(std::string_view sv) {
auto angle_bracket_mask =
sv | rv::transform([](auto e) { return e == '<' or e == '>'; });
return rv::zip(rv::zip_with(std::logical_or{},
angle_bracket_mask,
angle_bracket_mask | rv::partial_sum(std::not_equal_to{})), sv)
| rv::filter([](auto t) { return not std::get<0>(t); })
| rv::transform([](auto t) { return std::get<1>(t); })
| ranges::to<std::string>;
}
With the operator:
auto filter_out_html_tags(std::string_view sv) {
return sv
|> transform($, [](auto e) { return e == '<' or e == '>'; })
|> zip_transform(std::logical_or{}, $, scan_left($, true, std::not_equal_to{}))
|> zip($, sv)
|> filter($, [](auto t) { return not std::get<0>(t); })
|> values($)
|> ranges::to<std::string>($);
}
Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code?
No
Will your feature suggestion automate or eliminate X% of current C++ guidance literature?
No
Describe alternatives you've considered.
Alternatives are discussed at length in the two papers referenced above.
The proposed operator enables a simpler left-to-right style as opposed to an inside-out style.
As soon as I see this, I think "hmm, like UFCS does?", and then I saw the example which looks suspiciously like UFCS for most of the cases.
Clarifying questions, just so I understand the question (I haven't had time to watch the talk).
Is the $ is the placeholder for where to put the left-hand side of the operator, so that this code (ignoring for now the zip_transform one where it's not in the first location):
return sv
|> transform($, [](auto e) { return e == '<' or e == '>'; })
|> zip($, sv)
|> filter($, [](auto t) { return not std::get<0>(t); })
|> values($)
|> ranges::to<std::string>($);
would be the same as this using UFCS, which I think would work in Cpp2 now:
return sv
.transform(:(e) e == '<' || e == '>';)
.zip(sv)
.filter(:(t) !std::get<0>(t);)
.values()
.ranges::to<std::string>();
or something like that, modulo any late-night typos I wrote?
There's some overlap between UFCS and this proposed pipeline operator.
Conor's presentation shows off various range pipeline examples, but the P2011 and P2672 papers discuss the motivation and problem space.
P2011 also discusses how it's different to UFCS. I think the key difference for Cpp2 is allowing the placeholder to appear in different argument positions in order to compose the range algorithms and views.
Yes -- in a racing update I was updating the comment to say the following, but I'll make it a separate reply instead:
Right, that code is currently rejected because . must be followed by a name.
So perhaps have a general helper like call:(f) :(x) f(x); to enable writing
You're right that UFCS is very close already, and arguably the dot syntax is just as nice as the new operator, but with UFCS I'd still prefer a token to make the syntax simpler. (Perhaps _ is better than Circle's choice of $ since there's already precedent in Cpp2.)
I think |> is a valuable proposal to consider, it's like UFCS with explicit this, but with a new syntax which it doesn't have any advantage to existing code:
a |> function($, b, c) // Proposal
a . function( b, c) // UFCS
Cpp2 can have the good part of the proposal: The placeholder $. The placeholder allows us to use UFCS with explicit this.
The placeholder $ have to be changed to another syntax in Cpp2, because $ is the notation to capture variables. Let's assume the placeholder notation is it keyword in Cpp2.
Optional Placeholder
Let's consider the placeholder is optional.
If we don't pass it keyword as an argument in UFCS, this is implicitly the first argument.
Also it keyword can be generalized to refer to the left operand of a binary operation. For example:
var1: = (x + y) * (x + y);
// It's the same as above. `it` is `(x + y)`.
var2: = (x + y) * it;
// The first `it` is `(x + y)`.// The second `it` is `(x + y) * it`.
var3: = (x + y) * it * it;
var4: = ab.fnc1(it, it).fnc2(it, it);
var5: = ab.fnc1(ab).fnc2(ab.fnc1(ab));
And we can change the position of this argument in UFCS. In this example, this object is the last argument:
// : = fnc1(arg, obj);
var1: = obj.fnc1(arg, it);
This feature improves chained UFCS without requiring to use additional helper function call and lambda expression. Considering the example from the proposal, it will be changed with UFCS and it keyword in Cpp2:
If it's hard to find it keyword (although an editor with syntax highlighting can help in this case), another syntax such as $$ may be used.
The point of the placeholder is that:
It allows us to explicitly specify the position of this argument in UFCS.
It can be used to refer to the left operand of any binary operation too.
It doesn't require additional helper function call and lambda expression.
So I'm in favor of @bluetarpmedia's comment (but in a way that placeholder is optional):
You're right that UFCS is very close already, and arguably the dot syntax is just as nice as the new operator, but with UFCS I'd still prefer a token to make the syntax simpler. (Perhaps _ is better than Circle's choice of $ since there's already precedent in Cpp2.)
In another words, the placeholder is optional. If it keyword (or any other notation) is passed as an argument in UFCS, this argument will be passed in place of it:
var1: = ab.fnc1(mn, it);
// : = fnc1(mn, ab);
Otherwise this argument will be implicitly the first argument:
var1: = ab.fnc1(mn);
// : = fnc1(ab, mn);
So we can have chained function calls regardless of whether this is the first parameter or not.
If you find optional placeholders not to be readable, its readability may be increased by adding extra this like if it was a keyword argument (it depends on what will be the syntax of keyword arguments or designated constructors):
Suggestion to use 'lhs' instead of 'it' as keyword for this proposal. It is clearer as to the meaning of the value, and I found myself substituting 'it' for 'left hand side' in my head whenever I read it
On 15 November 2023 10:36:41 Sadeq ***@***.***> wrote:
Optional Placeholder Syntax
If you find optional placeholders not to be readable, it may increase readability by adding extra this like if it was a keyword argument (it depends on what will be the syntax of keyword arguments or designated constructors):
// : = obj.fnc1(this: = x, it);
// : = obj.fnc1(this? x, it);
var1: = obj.fnc1(this x, it);
Briefly A.F(this B, it) is equal to B.F(A) which it falls back to F(B, A). That's it. Thanks.
Without this, we cannot write it keyword in argument list:
var1: = obj.fnc1(x, it); // ERROR! `it` keyword requires explicit `this` argument.
And without it, UFCS will work as how we currently use it:
var1: = obj.fnc1(x); // OK. It falls back to `fnc1(obj, x)`.
So the placeholder doesn't conflict with the way UFCS works today, it complements it.
—
Reply to this email directly, view it on GitHub<#741 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQMNOYQDBIQY5TWNEDTYESLLLAVCNFSM6AAAAAA5ZQQZ32VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJSGIYTGNRQGI>.
You are receiving this because you commented.Message ID: ***@***.***>
Activity
hsutter commentedon Oct 10, 2023
As soon as I see this, I think "hmm, like UFCS does?", and then I saw the example which looks suspiciously like UFCS for most of the cases.
Clarifying questions, just so I understand the question (I haven't had time to watch the talk).
Is the
$is the placeholder for where to put the left-hand side of the operator, so that this code (ignoring for now the zip_transform one where it's not in the first location):would be the same as this using UFCS, which I think would work in Cpp2 now:
or something like that, modulo any late-night typos I wrote?
bluetarpmedia commentedon Oct 10, 2023
scan_leftis from code_report's example.hsutter commentedon Oct 10, 2023
Wow, that was fast. I was just trying to write the code for
zip_transforma different way, but I see a statement parameter worked.What about expressing this
as this
?
Then it's all
.?(I haven't tried to compile the code though.)
bluetarpmedia commentedon Oct 10, 2023
There's some overlap between UFCS and this proposed pipeline operator.
Conor's presentation shows off various range pipeline examples, but the P2011 and P2672 papers discuss the motivation and problem space.
P2011 also discusses how it's different to UFCS. I think the key difference for Cpp2 is allowing the placeholder to appear in different argument positions in order to compose the range algorithms and views.
hsutter commentedon Oct 10, 2023
Yes -- in a racing update I was updating the comment to say the following, but I'll make it a separate reply instead:
Right, that code is currently rejected because
.must be followed by a name.So perhaps have a general helper like
call:(f) :(x) f(x);to enable writingmodulo typos and bugs? Or maybe name it
curry? Anyway, signing off for tonight, but a very interesting question! Thanks.bluetarpmedia commentedon Oct 10, 2023
You're right that UFCS is very close already, and arguably the dot syntax is just as nice as the new operator, but with UFCS I'd still prefer a token to make the syntax simpler. (Perhaps
_is better than Circle's choice of$since there's already precedent in Cpp2.).zip_transform(std::logical_or{}, _, scan_left(_, true, std::not_equal_to{}))16 remaining items
SebastianTroy commentedon Nov 11, 2023
MaxSagebaum commentedon Nov 13, 2023
In my opinion, having a different symbol for UFCS would reduce surprises and would make it an opt-in. So I strongly support this notion.
msadeqhe commentedon Nov 15, 2023
I think
|>is a valuable proposal to consider, it's like UFCS with explicitthis, but with a new syntax which it doesn't have any advantage to existing code:Cpp2 can have the good part of the proposal: The placeholder
$. The placeholder allows us to use UFCS with explicitthis.The placeholder
$have to be changed to another syntax in Cpp2, because$is the notation to capture variables. Let's assume the placeholder notation isitkeyword in Cpp2.Optional Placeholder
Let's consider the placeholder is optional.
If we don't pass
itkeyword as an argument in UFCS,thisis implicitly the first argument.Also
itkeyword can be generalized to refer to the left operand of a binary operation. For example:And we can change the position of
thisargument in UFCS. In this example,thisobject is the last argument:// : = fnc1(arg, obj); var1: = obj.fnc1(arg, it);This feature improves chained UFCS without requiring to use additional helper function
calland lambda expression. Considering the example from the proposal, it will be changed with UFCS anditkeyword in Cpp2:If it's hard to find
itkeyword (although an editor with syntax highlighting can help in this case), another syntax such as$$may be used.The point of the placeholder is that:
thisargument in UFCS.calland lambda expression.So I'm in favor of @bluetarpmedia's comment (but in a way that placeholder is optional):
msadeqhe commentedon Nov 15, 2023
In another words, the placeholder is optional. If
itkeyword (or any other notation) is passed as an argument in UFCS,thisargument will be passed in place ofit:var1: = ab.fnc1(mn, it); // : = fnc1(mn, ab);Otherwise
thisargument will be implicitly the first argument:var1: = ab.fnc1(mn); // : = fnc1(ab, mn);So we can have chained function calls regardless of whether
thisis the first parameter or not.msadeqhe commentedon Nov 15, 2023
Optional Placeholder Syntax
If you find optional placeholders not to be readable, its readability may be increased by adding extra
thislike if it was a keyword argument (it depends on what will be the syntax of keyword arguments or designated constructors):Briefly
A.F(this B, it)is equal toB.F(A)which it falls back toF(B, A). That's it.Without
this, we cannot writeitkeyword in argument list:var1: = obj.fnc1(x, it); // ERROR! `it` keyword requires explicit `this` argument.And without
it, UFCS will work as how we currently use it:var1: = obj.fnc1(x); // OK. It falls back to `fnc1(obj, x)`.The example from the proposal will be like this (with new syntax):
So optional placeholder doesn't conflict with the way UFCS works today, it complements it. Thanks.
SebastianTroy commentedon Nov 15, 2023
Fixed terse syntax bug reported in hsutter#741 comments
Emit anonymous functions as `constexpr` or `mutable` lambdas
DerpMcDerp commentedon Feb 12, 2024
The unary pipeline operator can be used for terse lambda syntax, i.e.
|> foo($0, $2)==:(x, _, z) foo(x, z);Add `..` syntax to select a member function only (#1101)