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

mmix

Lets say I have a List<IEnumerable<double>> containing variable number of infinite sources of double numbers. Lets say they are all wave generator functions and I need to superimpose them into a single wave generator represented by IEnumerable<double> simply by taking the next number out of each and suming them.

I know I can do this through iterator methods, something like this:

    public IEnumerable<double> Generator(List<IEnumerable<double>> wfuncs)
    {
        var funcs = from wfunc in wfuncs
                    select wfunc.GetEnumerator();

        while(true)
        {
            yield return funcs.Sum(s => s.Current);
            foreach (var i in funcs) i.MoveNext();
        }
    } 

however, it seems rather "pedestrian". Is there a LINQ-ish way to achieve this?

mmix

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);
    }
    // ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

LINQ IEnumerable<T[]> to IEnumerable<T>

From Dev

IEnumerable<T> Merge

From Dev

List<T> vs IEnumerable<T>

From Dev

List<T> vs IEnumerable<T>

From Java

LINQ equivalent of foreach for IEnumerable<T>

From Dev

Can't transform Linq to IEnumerable

From Dev

implement ienumerable with ienumerable<T>

From Java

Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>

From Dev

List<T> vs IEnumerable<T> in foreach

From Dev

Assign List<T> to IEnumerable<T> in static method

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

I need a Linq IEnumerable<>.Cast(typeof(T))

From Dev

IEnumerable<Func<T,S>> and LINQ type inference

From Dev

Use of System.Linq in case of IEnumerable<t>?

From Dev

I need a Linq IEnumerable<>.Cast(typeof(T))

From Dev

ienumerable to ienumerable<t> without losses

From Dev

Casting an IEnumerable to IEnumerable<T> with reflection

From Dev

Passing list<T> or array as argument of type IEnumerable

From Dev

How to convert an IEnumerable<AnonymousType> to a List<T>?

From Dev

Select from IEnumerable<T> to IEnumerable<Quantity<T>>

From Dev

Turn an IObservable<IEnumerable<T>> into an IEnumerable<IObservable<T>>

From Dev

Adding IEnumerable<T> items to IEnumerable<T>

From Dev

Turn an IObservable<IEnumerable<T>> into an IEnumerable<IObservable<T>>

From Dev

cannot convert from IEnumerable<T> to IEnumerable<T>

From Dev

Select from IEnumerable<T> to IEnumerable<Quantity<T>>

From Dev

Why would one use IEnumerable<T> in an interface definition if the class implementing the interface will be using List<T>?

From Dev

Removing duplicate IEnumerable<T> from IEnumerable<IEnumerable<T>>

From Dev

Filter IEnumerable<T> in Model

Related Related

  1. 1

    LINQ IEnumerable<T[]> to IEnumerable<T>

  2. 2

    IEnumerable<T> Merge

  3. 3

    List<T> vs IEnumerable<T>

  4. 4

    List<T> vs IEnumerable<T>

  5. 5

    LINQ equivalent of foreach for IEnumerable<T>

  6. 6

    Can't transform Linq to IEnumerable

  7. 7

    implement ienumerable with ienumerable<T>

  8. 8

    Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>

  9. 9

    List<T> vs IEnumerable<T> in foreach

  10. 10

    Assign List<T> to IEnumerable<T> in static method

  11. 11

    IEnumerable<T> and IEnumerator - some clarification please

  12. 12

    IEnumerable<T> and IEnumerator - some clarification please

  13. 13

    I need a Linq IEnumerable<>.Cast(typeof(T))

  14. 14

    IEnumerable<Func<T,S>> and LINQ type inference

  15. 15

    Use of System.Linq in case of IEnumerable<t>?

  16. 16

    I need a Linq IEnumerable<>.Cast(typeof(T))

  17. 17

    ienumerable to ienumerable<t> without losses

  18. 18

    Casting an IEnumerable to IEnumerable<T> with reflection

  19. 19

    Passing list<T> or array as argument of type IEnumerable

  20. 20

    How to convert an IEnumerable<AnonymousType> to a List<T>?

  21. 21

    Select from IEnumerable<T> to IEnumerable<Quantity<T>>

  22. 22

    Turn an IObservable<IEnumerable<T>> into an IEnumerable<IObservable<T>>

  23. 23

    Adding IEnumerable<T> items to IEnumerable<T>

  24. 24

    Turn an IObservable<IEnumerable<T>> into an IEnumerable<IObservable<T>>

  25. 25

    cannot convert from IEnumerable<T> to IEnumerable<T>

  26. 26

    Select from IEnumerable<T> to IEnumerable<Quantity<T>>

  27. 27

    Why would one use IEnumerable<T> in an interface definition if the class implementing the interface will be using List<T>?

  28. 28

    Removing duplicate IEnumerable<T> from IEnumerable<IEnumerable<T>>

  29. 29

    Filter IEnumerable<T> in Model

HotTag

Archive