I said stay calm

I should have given an alternative solution in the last blog post. This uses Switch so that the timer is only subscribed to once. Switch is rather neat int hat it takes a sequence of observables and propagates the value of the latest element of this sequence.

var ticktock = Observable.Interval(TimeSpan.FromSeconds(1));

var clock =
    Observable.Switch(
      ticktock.Select(
         tick => Observable.Interval(TimeSpan.FromSeconds(0.1))
                 .Select(tenths => new { Tick = tick, Tenths = tenths })))
    .Select(anon => anon.Tick + "." + anon.Tenths);

clock.Subscribe(x => Console.WriteLine(x));

Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s