0

Consider the following code in Linqpad:

DateTime.Now.Dump();
Observable
    .Interval(TimeSpan.FromSeconds(3))
    //.Select(n => n + 1)
    //.StartWith(0)
    .Do(n => $"{DateTime.Now} {n}".Dump())
    .Wait();

It outputs:

1/22/2019 12:23:57 PM
1/22/2019 12:24:00 PM 0
1/22/2019 12:24:03 PM 1

Notice that the first element is observed 3 seconds later. I want it to be right away, which is on subscription, I suppose. My solution is:

DateTime.Now.Dump();
Observable
    .Interval(TimeSpan.FromSeconds(3))
    .Select(n => n + 1)
    .StartWith(0)
    .Do(n => $"{DateTime.Now} {n}".Dump())
    .Wait();

Which produces:

1/22/2019 12:26:10 PM
1/22/2019 12:26:10 PM 0
1/22/2019 12:26:13 PM 1
1/22/2019 12:26:16 PM 2

Note the first element is observed right away.

I was wondering if there is a more concise way to achieve this.

mark
  • 59,016
  • 79
  • 296
  • 580

0 Answers0