2

I have the following streams

T1---T2---T3---T4---T5---T6---T7---T8
Q1-Q3-Q7-Q10

I would like to combine them so it looks like this

Q1--------Q3------------------Q7-----

i,e, only release the event Qi when Ti is released.

Is there a simple way to achieve this using RX primitive?

3 Answers3

2

If your relationship is oneway only (that is T event occurs before matching Q one), you can achieve this with (js syntax to be confirmed):

tElts.selectMany(i -> qElts.takeWhile(n -> n<=i).where(n -> n==i))

The inner stream will return a single element stream Q3 for T3, and an empty stream for T2.

Gluck
  • 2,933
  • 16
  • 28
1

I am not sure if you are looking for a JavaScript solution or something in the System.Reactive namespace. I have this .NET solution that works. This solution assumes that the Q's will always be generated faster than the T's, i.e. Qn will never be generated after Tn for all n.

// Generate 8 T's, 1 per second.
IObservable<int> tSource = 
    Observable
    .Interval(TimeSpan.FromSeconds(1))
    .Select(x => (int)(x + 1))
    .Take(8);

// Generate 4 Q's immediately.
IObservable<int> qSource =
    (new int[] { 1, 3, 7, 10 })
    .ToObservable();

var result = 
    qSource
    .SelectMany(x => 
        tSource
        .Where(y => x == y)
        .Take(1));

Interesting note, this looks like the dual of Gluck's solution which also works.

Jason Boyd
  • 6,839
  • 4
  • 29
  • 47
0

The zip operator synchronizes multiple streams.

Brandon
  • 38,310
  • 8
  • 82
  • 87