I have
public static class TupleExtensions {
public static IEnumerable<T> SelectMany<T>(this IEnumerable<Tuple<T, T>> te)
{
foreach (var t in te)
{
yield return t.Item1;
yield return t.Item2;
}
}
public static IEnumerable<T> SelectMany<T, U>(this IEnumerable<U> source, Func<U, Tuple<T, T>> fn)
{
foreach (var s in source)
{
var t = fn(s);
yield return t.Item1;
yield return t.Item2;
}
}
public static IEnumerable<T> SelectMany<T, U>(this IEnumerable<U> source, Func<U, Tuple<T, T, T>> fn)
{
foreach (var s in source)
{
var t = fn(s);
yield return t.Item1;
yield return t.Item2;
yield return t.Item3;
}
}
public static IEnumerable<T> SelectMany<T, U>(this IEnumerable<U> source, Func<U, Tuple<T, T, T,T>> fn)
{
foreach (var s in source)
{
var t = fn(s);
yield return t.Item1;
yield return t.Item2;
yield return t.Item3;
yield return t.Item4;
}
}
}
I can then do as a trivial example
IEnumerable<int> source = ...
source.SelectMany(i=>Tuple.Create(i, i+1, i+2))
Obviously the tuple must have the same types throughout. My use case is where I have a mathematical solver which will always return two solutions for a given input. Returning a list makes it unclear how many solutions would be returned.
However in the end I would want all my solutions merged. For example
static Tuple<Point,Point> Intersect(this Shape shape, Line other);
Shape shape = ...
IEnumerable<Line> lines = ...
IEnumerable<Point> intersections = lines.SelectMany(line=>shape.Intersect(line));
Is there some good reason not to treat Tuple<T,T,T....>
as a container that we can flatten?
T[]
orReadOnlyCollection<T>
. \$\endgroup\$