Play recorded data stream in F #

I recorded real-time stock quotes in an SQL database with fields Id, Lastand TimeStamp. The last current share price (as double), and TimeStampthis DateTimeis when the price change was recorded.

I would like to reproduce this stream in the same way as it appeared, which means that if the price change was initially at a distance of 12 seconds, then the change in the price change event (or something like that) should be 12 seconds.

In C #, I can create a collection, sort it by DateTime, and then fire the event using the time difference to find out when to change the next price change. I understand that F # has a lot of interesting new things related to events, but I don’t know how to start this in F #. Any thoughts / code snippets / useful links on how I can continue this?

+3
source share
1 answer

I think you will like the F # solution :-).

To simplify the example, I save the price and timestamps in a list containing tuples (the first element is the delay from the last update, and the second element is the price). It should not be too difficult to convert your input into this format. For instance:

let prices = [ (0, 10.0); (1000, 10.5); (500, 9.5); (2500, 8.5) ]

, . , :

let evt = new Event<float>()
evt.Publish.Add(printfn "Price updated: %f")

- - , , , :

async { for delay, price in prices do
          do! Async.Sleep(delay)
          evt.Trigger(price) }
|> Async.StartImmediate

StartImmediate, , ( , ). (, ).

EDIT. - , , :

type ReplyDataStream(prices) =
  let evt = new Event<float>()
  member x.Reply() = 
    // Start the asynchronous workflow here
  member x.PriceChanged = 
    evt.Publish

, , stream.PriceChanged.Add(...), Reply()

+10

All Articles