Here is a candidate solution which passes the above test. However I'm not rock certain that it satisfies requirements.
/// <summary>
/// Pass the head and tail of the observable to the
/// selector function. Note that
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
/// <param name="source"></param>
/// <param name="fn"></param>
/// <returns></returns>
public static IObservable<U> HeadTailSelect<T, U>
(this IObservable<T> source, Func<T, IObservable<T>, U> fn)
{
var tail = new Subject<T>();
return Observable.Create<U>(observer =>
{
return source.Subscribe(v =>
{
tail.OnNext(v);
var u = fn(v, tail);
observer.OnNext(u);
}
,e=> { tail.OnCompleted();observer.OnError(e); }
,()=> { tail.OnCompleted();observer.OnCompleted(); });
});
}
Note that u
is most likely to be some kind of IObservable
and should be subscribed to immediately. If this is done I think everything should be ok.