Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rx: EnumerableEx.For() vs Enumerable.SelectMany()

System.Interactive.dll includes a For() method with the following implementation:

IEnumerable<TResult> For<TSource, TResult>(
    IEnumerable<TSource> source,
    Func<TSource, IEnumerable<TResult>> resultSelector)
{
    return source.Select<TSource, IEnumerable<TResult>>(resultSelector).Concat<TResult>();
}

Am I missing something or is this equivalent to the existing Enumerable.SelectMany(), minus this?

IEnumerable<TResult> SelectMany<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, IEnumerable<TResult>> selector)
like image 223
dahlbyk Avatar asked Oct 06 '10 03:10

dahlbyk


1 Answers

Good question. They produce the same results but the internal implementations are quite different.

EnumerableEx.For would have been added to System.Interactive to maintain duality between IObservable and IEnumerables. Note that Observable.For and Observable.SelectMany are different:

IObservable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IObservable<TResult>> resultSelector)

vs.,

IObservable<TResult> SelectMany<TSource, TResult>(this IObservable<TSource> source, Func<TSource, IObservable<TResult>> selector)

Therefore you'd expect that EnumerableEx.For would have this signature, rather than the one it actually has:

IEnumerable<TResult> For<TSource, TResult>(**IObservable**<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)

However, it obviously doesn't take an IObservable source. Perhaps it was meant to. I would ask your question in the Rx forums, to see if the Rx team has an answer.

like image 111
Richard Anthony Freeman-Hein Avatar answered Sep 25 '22 01:09

Richard Anthony Freeman-Hein