Skip to content

Spread tuples: [(x, x), (x, x), …] → [x, x, x, x, …] #939

Open
@atifaziz

Description

@atifaziz

I propose to add a new operator that takes a sequence of tuples and spreads the elements of each tuple into the resulting sequence:

public static IEnumerable<T> Spread<T>(this IEnumerable<(T, T)> source)
{
    foreach (var (a, b) in source)
    {
        yield return a;
        yield return b;
    }
}

Additional overloads can be supplied for remaining multary tuple types.

Example:

using MoreLinq;

var xs =
    Enumerable.Range(1, 10)
              .Zip(MoreEnumerable.Return(0).Repeat())
           // ...above same as...
           // .Select(x => (x, 0))
              .Spread();

foreach (var x in xs)
    Console.WriteLine(x);

Outputs:

1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
0

This would be more generally usable than what's proposed in #695.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions