1

Today I finally stumbled upon the solution of a really trivial RX problem: Suppose you have an Observable which returns Lists of items. Like Observable<List<String>>. You often receive something like this as responses from web APIs.

However chances are you want to operate on the single items, in this case the Strings.

flatMapIterable to the rescue! This handy operator flattens a stream of Iterables into a stream generated from the single items of these Iterables by means of a mapping function.

RxJava: flattening a stream of Iterables | by Sven Bendel

I'm writing in .NET Core if that matters.

Example in RxJava: Convert Observable<List<Car>> to a sequence of Observable<Car> in RxJava

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Chip
  • 1,439
  • 3
  • 15
  • 29
  • Fun fact: the operation you want is the bind operation on the observable monad. – Eric Lippert Feb 26 '18 at 19:10
  • @EricLippert, I was asking for a specific version of bind called flatMapIterable (in RxJava). It appears this is not available in Rx.NET. – Chip Feb 27 '18 at 01:08
  • It is available ```listEmittingObservable.SelectMany(item => item)``` – Chip Mar 01 '18 at 02:30

3 Answers3

5

The LINQ operation to map a collection-of-collection-of-items into an aggregated collection of items is SelectMany. This operation exists in System.Reactive as well, allowing you to create a single Observable from a collection of Observables:

http://www.introtorx.com/content/v1.0.10621.0/08_Transformation.html#SelectMany

Observable.Range(1,3)
          .SelectMany(i => Observable.Range(1, i))
          .Dump("SelectMany");

...we will now get an output with the result of each sequence ([1], [1,2] and [1,2,3]) flattened to produce [1,1,2,1,2,3].

SelectMany-->1
SelectMany-->1
SelectMany-->2
SelectMany-->1
SelectMany-->2
SelectMany-->3
SelectMany completed
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
  • Thanks! But my question was for a flatMapIterable whereas SelectMany is just flatMap. Check out the article and the SO question I linked to for an explanation. It appears there is no Rx.NET equivalent so I'll just stick with combining SelectMany and .ToObservable. – Chip Feb 27 '18 at 00:51
  • This doesn't answer the question at all. The question is asking how to convert `IObservable>` to `IObservable`. – Enigmativity Feb 28 '18 at 03:03
  • It wasn't clear to me at first until @Shlomo explained. I updated my answer with code matching the flatMapIterable example: ```listEmittingObservable.SelectMany(item => item)```. – Chip Mar 01 '18 at 02:29
0

It really is trivial to to do this in C#. Just do this:

IObservable<IList<int>> source =
    Observable.Return(new [] { 1, 2, 3 }.ToList());

IObservable<int> output =
    from list in source
    from item in list
    select item;

Simple.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
-1

Edit: My mistake, it's also SelectMany as an overloaded method:

public IObservable<string> IterateObservable(IList<string> strings)
{
    return strings
        .ToObservable()
        .Buffer(100) // IObservable<IList<string>>
        .SelectMany(item => item); // IObservable<string>
}

It appears there is no equivalent to flatMapInterable in Rx.NET so you must stick with combining SelectMany and ToObservable. Plus more to actually apply a transformation to each item.

Example (same as the RxJava example in the stackoverflow link):

public IObservable<string> IterateObservable(IList<string> strings)
{
    return strings
        .ToObservable()
        .Buffer(100)
        .SelectMany(list => list.ToObservable());
}

To perform an operation on it similar to flatMapIterable (but more verbose of course):

public IObservable<string> IterateObservable(IList<string> strings)
{
    return strings
        .ToObservable()
        .Buffer(100)
        .SelectMany(
                list => list.ToObservable()
                    .Select( stringInList => stringInList.Trim()));
}
Chip
  • 1,439
  • 3
  • 15
  • 29
  • This is incorrect, the other answer is correct. There are overloads of SelectMany that accept a Func to IEnumerable (the .NET equivalent of Iterable). – Shlomo Feb 27 '18 at 12:47
  • Ohh, it's an overload that makes sense! My mistake – Chip Mar 01 '18 at 02:24