Ivan
Ivan

Reputation: 7746

Zip a Grouped IObservable

public delegate void QuoteChangeEvent(IQuote q);

var priceChangedObservable = Observable.FromEvent<QuoteChangeEvent, IQuote> 
    (handler =>
    {
        QuoteChangeEvent qHandler = (e) =>
        {
            handler(e);
        };

        return qHandler;
    },
    qHandler => api.MAPI.OnQuoteChange += qHandler,
    qHandler => api.MAPI.OnQuoteChange -= qHandler);

    var grouped = priceChangedObservable
    .GroupBy(instrument => instrument.Symbol);

So grouped is of type IObservable<IGroupedObservable<string, IQuote>>

Two questions.

1) I tried to

grouped.SortBy(instrument => instrument.Symbol);

But SortBy does not seem to exist?

2) Say there are two symbols that come in, GOOG and AAPL into grouped. How do I use the Zip operator so that what I get when I Subscribe is a Tuple<IQuote, IQuote> ?

I can't quite get the right syntax. Something like:

Observable.Zip(?, ?, (a, b) => Tuple.Create(a, b))
    .Subscribe(tup => Console.WriteLine("Quotes: {0} {1}", tup.item1, tup.item2));

EDIT 1

I almost got it:

    var first = grouped.Where(group => group.Key == "GOOG").FirstAsync();
    var second = grouped.Where(group => group.Key == "AAPL").FirstAsync();

    Observable.Zip(first, second, (a, b) => Tuple.Create(a, b))
    .Subscribe(tup => Console.WriteLine("Quotes: {0} {1}", tup.Item1, tup.Item2));

The problem is that tup is not of type <IQuote, IQuote>, but of type:

Tuple<IGroupedObservable<string, IQuote>, IGroupedObservable<string, IQuote>>

Upvotes: 0

Views: 110

Answers (1)

Enigmativity
Enigmativity

Reputation: 117027

How does this look to you?

IObservable<(IQuote, IQuote)> results =
    grouped
        .Publish(gs =>
            gs
                .Where(g => g.Key == "GOOG")
                .SelectMany(g => g)
                .Zip(
                    gs
                        .Where(g => g.Key == "AAPL")
                        .SelectMany(g => g),
                    (x, y) => (x, y)));

Upvotes: 0

Related Questions