LINQ merge List<IEnumerable<T>> into one IEnumerable<T> by some rule

You could aggregate the Zip-method over the IEnumerables.

    public IEnumerable<double> Generator(List<IEnumerable<double>> wfuncs)
    {
        return wfuncs.Aggregate((func, next) => func.Zip(next, (d, dnext) => d + dnext));
    }

What this does is bascically applies the same Zip-method over and over again. With four IEnumerables this would expand to:

wfuncs[0].Zip(wfuncs[1], (d, dnext) => d + dnext)
         .Zip(wfuncs[2], (d, dnext) => d + dnext)
         .Zip(wfuncs[3], (d, dnext) => d + dnext);

Try it out: fiddle


I guess there is no way around this without extending LINQ. So here's what I wrote in the end. I'll try to contact MoreLinq authors to get this included in some way, it can be useful in some pivoting scenarios:

public static class EvenMoreLinq
{
    /// <summary>
    /// Combines mulitiple sequences of elements into a single sequence, 
    /// by first pivoting all n-th elements across sequences 
    /// into a new sequence then applying resultSelector to collapse it
    /// into a single value and then collecting all those 
    /// results into a final sequence. 
    /// NOTE: The length of the resulting sequence is the length of the
    ///       shortest source sequence.
    /// Example (with sum result selector):
    ///  S1   S2   S2    |  ResultSeq
    ///   1    2    3    |          6 
    ///   5    6    7    |         18
    ///  10   20   30    |         60
    ///   6    -    7    |          -
    ///   -         -    |          
    /// </summary>
    /// <typeparam name="TSource">Source type</typeparam>
    /// <typeparam name="TResult">Result type</typeparam>
    /// <param name="source">A sequence of sequences to be multi-ziped</param>
    /// <param name="resultSelector">function to compress a projected n-th column across sequences into a single result value</param>
    /// <returns>A sequence of results returned by resultSelector</returns>
    public static IEnumerable<TResult> MultiZip<TSource, TResult>
                                  this IEnumerable<IEnumerable<TSource>> source, 
                                  Func<IEnumerable<TSource>, TResult> resultSelector)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (source.Any(s => s == null)) throw new ArgumentNullException("source", "One or more source elements are null");
        if (resultSelector == null) throw new ArgumentNullException("resultSelector");

        var iterators = source.Select(s => s.GetEnumerator()).ToArray();
        try
        {
            while (iterators.All(e => e.MoveNext()))
                yield return resultSelector(iterators.Select(e => e.Current));
        }
        finally
        {
            foreach (var i in iterators) i.Dispose();
        }
    }
}

using this I managed to compress my combined generator:

interface IWaveGenerator
{
    IEnumerable<double> Generator(double timeSlice, double normalizationFactor = 1.0d);
}


[Export(typeof(IWaveGenerator))]
class CombinedWaveGenerator : IWaveGenerator
{
    private List<IWaveGenerator> constituentWaves;

    public IEnumerable<double> Generator(double timeSlice, double normalizationFactor = 1)
    {
        return constituentWaves.Select(wg => wg.Generator(timeSlice))
                               .MultiZip(t => t.Sum() * normalizationFactor);
    }
    // ...
}

This is a situation where LINQ would probably be more difficult to understand, and not buy you anything. Your best bet is to just fix your sample method. Something like this should work:

public IEnumerable<double> Generator(IReadOnlyCollection<IEnumerable<double>> wfuncs)
{
    var enumerators = wfuncs.Select(wfunc => wfunc.GetEnumerator())
        .ToList();

    while(enumerators.All(e => e.MoveNext()))
    {
        yield return enumerators.Sum(s => s.Current);
    }
} 

Tags:

C#

Linq