Skip to content

Proposal: New Zip LINQ Tuple Overload API #20167

Closed

Description

@jcouv

(By @agocke) Rewriting this issue to focus on exactly one API:

class System.Linq.Enumerable
{
    public static IEnumerable<(T1, T2)> Zip<T1, T2>(this IEnumerable<T1> first, IEnumerable<T2> second)
    {
        return first.Zip(second, (x, y) => (x, y));
    }
    // Already EXISTS:
    public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector);
}

There will also be a matching IQueryable API:

class System.Linq.Queryable
{
    public static IQueryable<(T1, T2)> Zip<T1, T2>(this IQueryable<T1 first, IQueryable<T2> second);
    // Already EXISTS:
    public static IQueryable<TResult> Zip<TFirst, TSecond, TResult>(this IQueryable<TFirst> source1, IEnumerable<TSecond> source2, Expression<Func<TFirst, TSecond, TResult>> resultSelector);
}

[EDIT] Add class names and existing Zip method for context by @karelz

Activity

  1. self-assigned this
    on Feb 9, 2017
  2. svick commented on Feb 9, 2017

    @svick
    Member

    How would Unzip work when it comes to laziness? Would it be similar to GroupBy, which I belive iterates the whole input before yielding the first result? Or something more lazy, where iterating first would iterate input at the same time, while saving second results to a queue (and vice versa)?

    Also, considering that with this change, there would be tuple and non-tuple Zip, but only tuple Unzip, would it make sense to also add "non-tuple" Unzip? The API would be:

    (IEnumerable<T1> first, IEnumerable<T2> second) Unzip<TInput, T1, T2>(this IEnumerable<TInput> input, Func<TInput, (T1, T2)> selector)

    (It does use tuple, but only for multiple returns, not for the collection itself.)

  3. karelz commented on Feb 10, 2017

    @karelz
    Member

    @jcouv please check our issue guide for assigning issues. And API review process.

  4. VSadov commented on Jan 23, 2018

    @VSadov
    Member
  5. agocke commented on Jan 25, 2018

    @agocke
    Member

    I've create a PR (dotnet/corefx#26582) that adds the Zip functionality described in the issue.

    One thing that I'm considering but haven't decided upon: would it be OK to return a concrete struct-iterator type instead of IEnumerable? It seems like forcing the IEnumerable overhead is something that many users would prefer not to do.

  6. agocke commented on Jan 25, 2018

    @agocke
    Member

    Note: I have not implemented Unzip. I don't think that API is nearly as useful and I'd prefer to decide on the new Zip API without it.

  7. agocke commented on Jan 25, 2018

    @agocke
    Member

    @VSadov and @OmarTawfik you're listed as experts for System.Linq. What would you like me to do here? Just publish a PR? The Zip API from above is still accurate.

  8. OmarTawfik commented on Jan 25, 2018

    @OmarTawfik
    Contributor
  9. svick commented on Jan 25, 2018

    @svick
    Member

    @agocke

    would it be OK to return a concrete struct-iterator type instead of IEnumerable? It seems like forcing the IEnumerable overhead is something that many users would prefer not to do.

    For better or worse, returning IEnumerable<T> is the pattern LINQ uses. What makes Zip special? Why should it be different?

  10. changed the title [-]Proposal: Zip and Unzip LINQ APIs [/-] [+]Proposal: New Zip LINQ Tuple Overload API[/+] on Feb 14, 2018
  11. agocke commented on Feb 14, 2018

    @agocke
    Member

    @karelz I think this is ready for review. I want to add exactly one IEnumerable API: the overload of Zip`2 that returns an IEnumerable of 2-tuples.

  12. karelz commented on Feb 14, 2018

    @karelz
    Member

    @agocke please get agreement with @VSadov @OmarTawfik first - they should mark it ready for review.

  13. OmarTawfik commented on Feb 16, 2018

    @OmarTawfik
    Contributor

    @karelz I think this is ready to be reviewed next week.

  14. VSadov commented on Feb 16, 2018

    @VSadov
    Member

    @karelz - 👍 we should review this.

  15. terrajobst commented on Apr 24, 2018

    @terrajobst
    Contributor

    Video

    ❤️

  16. Neme12 commented on May 19, 2018

    @Neme12

    Should the result be a named tuple? (TFirst first, TSecond second) for symmetry with the parameters?

  17. agocke commented on May 19, 2018

    @agocke
    Member

    @Neme12 We could, although tbh i don't see much difference between "first" and "Item1".

    People could give more useful names by saying IEnumerable<(int key, int value)> zipped = Zip(...)

  18. Thaina commented on Feb 6, 2019

    @Thaina

    If we would added API for Zip I would like to have Zip overload with more than 2 variables too

    foreach(var (a,b,c) in AItems.Zip(BItems,CItems))
    {
    }
    // should be the same as
    foreach(var (a,(b,c)) in AItems.Zip(BItems.Zip(CItems)))
    {
    }
  19. RyotaMurohoshi commented on May 11, 2019

    @RyotaMurohoshi
  20. bbartels commented on Oct 20, 2019

    @bbartels
    Contributor

    How come this didn't make its way into .NET Standard 2.1? Was hit with a compile error when switching a console app (netcoreapp3.0) to a class lib(netstandard2.1). I reverted to the old .Zip(), so just wondering why it didn't make the cut.

  21. stephentoub commented on Oct 20, 2019

    @stephentoub
    Member

    How come this didn't make its way into .NET Standard 2.1?

    @terrajobst?

  22. transferred this issue fromdotnet/corefxon Jan 31, 2020
  23. added this to the 3.0 milestone on Jan 31, 2020
  24. bbartels commented on Feb 10, 2020

    @bbartels
    Contributor

    How come this didn't make its way into .NET Standard 2.1?

    With .NET 5 coming up I guess it is not the most important question to be answered (as I assume 2.1 is the last version?), I am still curious though. @terrajobst

  25. terrajobst commented on Feb 10, 2020

    @terrajobst
    Contributor

    The short answer is timing. Coordinating changes between two ongoing release trains is challenging, especially when things change after merging. Hence, our generally policy for .NET Standard has been that we only standardize what was shipped before. During .NET Standard 2.1, we have relaxed that requirement to ship some of the high-impact features together with .NET Core 3.0.

  26. ghost locked as resolved and limited conversation to collaborators on Dec 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Linq

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions