2

In short

Looking for a way to achieve

observableOne.SkipIf(observableTwoFiresWithinPeriod)

Explanation

  • Let's say I am observing people walking through a door (walkins Observable)
  • and I also have a way to tell if they have a phone (phoneWalkins Observable).
  • Now I would like to observe the other piece of the pie of people walking through the door without a phone (nonPhoneWalkins Observable)

Given I have walkins and the phoneWalkins subset available to me as streams then nonPhoneWalkins should be walkins except phoneWalkins.

  • How to implement that with Reactive Extensions?
  • Note that phoneWalkins signals just after walkins if the person is found to carry a phone
Community
  • 1
  • 1
Cel
  • 6,467
  • 8
  • 75
  • 110

2 Answers2

1

Here is my simple try:

public class Program
    {
        static void Main()
        {
            // all walkins
            var walkins =
                // all persons through a door each second
                Observable.Interval(TimeSpan.FromSeconds(1), Scheduler.CurrentThread)
                // if has phone (is even) then true, if not false
                .Select(i => (i % 2 == 0));

            var phoneWalkins = // substream of phoneWalkins (if you want it separate)
                walkins
                .Where(i => (i == true))
                .Select(i => i);

            var nonPhoneWalkins = // substream of nonPhoneWalkins (if want it separate)
                walkins
                .Where(i => (i == false))
                .Select(i => i);

            //walkins.Subscribe(Console.WriteLine); // output all
            phoneWalkins.Subscribe(Console.WriteLine); // output phone
            //nonPhoneWalkins.Subscribe(Console.WriteLine); // output nonPhone
        }
    }
Bad
  • 4,967
  • 4
  • 34
  • 50
  • Unfortunately that is not the situation I have meaning phoneWalkins and nonPhoneWalkins are not directly derivable from walkins i.e. phoneWalkins is an independent measurement source e.g. Bob is hired to count people walking in with phones but a CCTV camera counts walk-ins indiscriminately – Cel Mar 28 '16 at 09:49
1

Thanks @supertopi for pointing to another relevant question, and the answer of @LeeCampbell there ported to this context should work:

nonPhoneWalkins = 
    walkins.SelectMany
    (walkin => 
        phoneWalkins.Take(1).Select(phoneWalkin => false)
        .Amb(
            Observable.Timer(TimeSpan.FromSeconds(1)).Select(l => true)
            )
    )
    .Where(nonPhoneWalkin => nonPhoneWalkin);
Cel
  • 6,467
  • 8
  • 75
  • 110