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

[Proposal] Add a Zip LINQ query operator #535

Open
AdamSpeight2008 opened this issue Apr 21, 2020 · 5 comments
Open

[Proposal] Add a Zip LINQ query operator #535

AdamSpeight2008 opened this issue Apr 21, 2020 · 5 comments

Comments

@AdamSpeight2008
Copy link
Contributor

AdamSpeight2008 commented Apr 21, 2020

Documentation on ZIP method.
Prior Art: Roslyn-8221
Roslyn-100

From #525, separating out the discussion around a Zip operator

Dim  xs = {0, 1, 2, 4}
Dim  ys = {"A"c, "B"c, "C"c, "D"c, "E"c}
Dim  zs = From x In xs Zip y In ys
          Select New With {.x = x, .y = y)

Output

{ x = 0, y = A }
{ x = 1, y = B }
{ x = 2, y = C }
{ x = 4, y = D }

This would be lowered in to a call to Enumerable.Zip function. eg.

Dim zs = Enumerable.Zip(xs,ys, Function(x,y) (x,y) )

Implementation

Definition of ZipKeyword

This will be contextual keyword, expressible within a LINQ Query Clause.

<node-kind name="ZipKeyword" token-text="Zip" />

Definition of ZipClauseSyntax

<node-structure name="ZipClauseSyntax" parent="QueryClauseSyntax">
    <node-kind name ="ZipClause" />
    <child name="ZipKeyword" kind="ZipKeyword" />
    <child name="ZipWith" kind="CollectionRangeVariable" />
</node-structure>

Refactor out the parsing CollectionRangeVariable from ParseFromControlVar, so it can also be utilised by the `ParseZipClause" function,

Private Function ParseZipClause(zipKw As KeywordSyntax) As QueryClauseSyntax
    Debug.Assert(zipKw IsNot Nothing, "Expected ZIP keyword.")
    Dim rangeVar = ParseCollectionRangeVariable()
    Dim zipOp = InternalSyntaxFactory.ZipClause(zipKw, rangeVar)
     zipOp = CheckFeatureAvailability(Feature.ZipQueryExpression, zipOp)
    Return zipOp
End Function
@aarondglover
Copy link

I don't understand the proposed function of the Zip operator... Can you explain for us mere mortals?

@AdamSpeight2008
Copy link
Contributor Author

@aarondglover updated original post.

@RevensofT
Copy link

Look great but personally I prefer to use Zip instead Select or just use Select do the zip.

Dim  zs = From x In xs, y In ys Zip (x, y)
Dim  ss = From x In xs, y In ys Select (x, y)

@AdamSpeight2008
Copy link
Contributor Author

AdamSpeight2008 commented Apr 25, 2020

@RevensofT
I don't want the cross-product of the two sequences, which produces 16 items.
Instead I want to merge the two sequences in to single sequence, of 5 items.

@VBAndCs
Copy link

VBAndCs commented Apr 25, 2020

Great, but I can suggest a more compact syntax using tuple-alike syntax:

   Dim zs = From (x, Y) In (xs, ys)

Note that Select (x, y) is optional, since we return the exact iterator tuple. if you want to return other result:

   Dim zs = From (x, Y) In (xs, ys)
                  Select x * y

In fact I hope to generalize this tuple iterator for all loops:

For each (x, y) in (A, B)
    Console.WriteLine(x & y)  
Next

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

No branches or pull requests

4 participants