Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Reactive Extensions API in depth: Primitives

Download

Right click “Save as…”

Jeff describes the primitives of IObservable.

Tags:

Follow the Discussion

  • Does Return call OnNext after someone subscribes, or calls it OnNext during the call to Subscribe? If I call Subscribe, I get back an IDisposable, but do I get the change to call Dispose before the Observable starts pushing values?

     

    In other words, what if I want this method:

    IObservable<T> ToObservable<T>(this IEnumerable<T> xs)
    {
      return new IObservable<T>()
      {
        IDisposable Subscribe(IObserver<T> obs)
        {
          foreach (var x in xs)
            obs.OnNext(x);
          obs.OnDone();
          return someDisposable(); // Don't know how to implement
        }
      }
    }

     

    Can I unsubscribe from the IObservable before or during the series of OnNext calls?

  • Bent Rasmussenexoteric stuck in a loop, for a while

    Looks like these definitions

     

    never :: () -> ⊥
    empty :: () -> ()
    return :: a -> () -> a
    throw :: Exception a => a -> () -> a

     

    That is,

     

    T Never<T>()
    {
        while (true);
        return default(T);
    }
    void Empty()
    {
    }
    Func<T> Return<T>(T x)
    {
        return () => x;
    }
    Action Throw<T>(T x) where T: Exception
    {
        return () => { throw x; };
    }
    

     

    Primitives

     

    var s1 = Observable.Never<int>();
    var s2 = Observable.Empty<int>();
    var s3 = Observable.Return<int>(42);
    var s4 = Observable.Throw<int>(new Exception());

     

    Primitives - Asynchronous

     

    var a1 = Observable.Start(() => { while (true); });
    var a2 = Observable.Start(() => { });
    var a3 = Observable.Start(() => 42);
    var a4 = Observable.Start(() => { throw new Exception(); });

     

    Need to try out but that looks about right to me...

  • Bottom isn't really a type. I'd say something like:

     

    never :: Subject a
    empty :: Subject a
    return :: a -> Subject a
    throw :: Exception e => e -> Subject a

     

    Where of course:

     

    instance Monad Subject where 
     ...

  • Bent Rasmussenexoteric stuck in a loop, for a while

    If it doesn't terminate, does the type matter, except that it's "bottom" - which should be every type as well?

     

    The reason I write it like functions is because if we look at an IObservable then it looks like a statement sequence to me, especially after the Beckman interview where I believe Erik showed this connection in that you could model an imperative program of sequences of statements as these things.

     

    s1; s2; s3;

     

    The primitives are then ways to model basic ways that a statement can behave. Some statements never return. Some statements are constant assignments (or we could say assignment expressions). Some statements throw exceptions. Some statements never terminate. To Erik the last two would be somewhat similar according to previous answers.

     

    And so the first functions defined would mimic this behavior in that you could write

     

    var s1 = Never(); // s1 is bottom, doesn't ever return
    Empty<int>(); // void - we can't assign that
    var s3 = Return(42)(); // s3 = 42
    Throw(new ForFunException("hah hah"))(); // bottom again

     

    I guess one of the reasons for providing Never out of the box (that is, in the box), is that if you can be more clever about the implementation instead of saying while(true); simply never call OnNext or OnComplete; no reason to make the CPU hot.

  • Good question.

     

    We have gone back and forth on this issue over time, but I expect in the long run that the user can choose either one since they both have meaningful semantics.

  • In Haskell ⊥ has type forall a :: a.

     

    bottom :: a

    bottom = bottom

  • All Rx combinators have non-blocking subscribe, so OnNext is called on the SynchronizationContext. For Return's case this means that if you subscribe on the thread owning the SynchronizationContext you're guaranteed to be called after Subscribe returns, or if you're calling from a different thread, you're called either during or after your call to subscribe. In the first case, calling Dispose should guarantee not to make the OnNext call, where the latter you might or might not succeed to cancel subscription.

  • Hi,

    I am working on silverlight application.

    I have an error on following code snips

    Please ans .. 

     

    Observable.Start(() =>
                            {
                                Thread.Sleep(3000);
                                return "Hello There";
                            }).Subscribe(Value => button.Content = Value);
                    };

     

    this code is not working VS2008 with silverlight application.

    It raised error "Specified method is not supported"

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.