-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
With clause in query expressions (support for zip) #8221
Comments
I tried implementing this, and it looks like making |
@orthoxerox Utilizing extension implementations (#8127): // assuming covariant tuples
extension<T, U> (IEnumerable<T>, IEnumerable<U>) : IEnumerable<(T, U)> {
IEnumerator<(T, U)> GetEnumerator() {
return this.Item1.Zip(this.Item2, (x, y) => (x, y)).GetEnumerator();
}
}
// tada
var result =
from tuple in (list1, list2)
select tuple case (var i, var j) : i + j;
// if we could use patterns in linq
var result =
from (var i, var j) in (list1, list2)
select i + j; Today you can do this the uninteresting way, via a regular extension method, static IEnumerable<Tuple<T, U>> Zip<T, U>(this Tuple<IEnumerable<T>, IEnumerable<U>> source) {
return source.Item1.Zip(source.Item2, Tuple.Create);
}
var result =
// explicit type arguments are required because tuples are invariant
from tuple in Tuple.Create<IEnumerable<int>, IEnumerable<int>>(list1, list2).Zip()
select tuple.Item1 + tuple.Item2; |
What's wrong with using |
@AdamSpeight2008 someone has |
@orthoxerox Having Imports System.Runtime.CompilerServices
Module Module1
Sub Main()
Dim e1 = {1, 2, 3, 4}
Dim e2 = {"A"c, "B"c, "C"c, "D"c}
Dim ie = e1.ZipT(e2).Select(Function(t)
Dim x = t.Item1
Dim y = t.Item2
Return New String(y, x)
End Function)
End Sub
<Extension>
Function ZipT(Of T1, T2)(e1 As IEnumerable(Of T1), e2 As IEnumerable(Of T2)) As IEnumerable(Of Tuple(Of T1, T2))
Return e1.Zip(e2, Function(x, y) Tuple.Create(x, y))
End Function
End Module Using Dim ie = Zip x In e1, y In e2 Into New String(y,x) |
Old discussion #100 |
I've devised the following grammar definition for
Suggests that adding a Query Syntax for |
@orthoxerox I have to ask, why there is a need for a new contextual keyword, from tuple in (list1, list2) select ... Isn't good enough? |
@alrz This would require tuples to support LINQ functions that turn them inside out, and we can't do that without HKT/traits:
|
Moved to dotnet/csharplang#117 |
I propose to add a
with
clause to the query expression syntax. This will allow us to zip sequences together using either LINQ syntax.A query expression with a
with
clause followed by aselect
clauseis translated into
A query expression with a
with
clause followed by something other than aselect
clauseis translated into
This transformation would happen in section 7.16.2.4 of the spec:
is first translated into
and then translated into (if I understand how transparent identifiers work)
And
is first translated into
and then translated into
The text was updated successfully, but these errors were encountered: