public static Func <T1, T2, IObservable <TResult> > FromAsyncPattern <T1, T2, TResult>(Func <T1, T2, AsyncCallback, object, IAsyncResult> begin, Func <IAsyncResult, TResult> end)
 {
     return((x, y) =>
     {
         var subject = new AsyncSubject <TResult>();
         try
         {
             begin(x, y, iar =>
             {
                 TResult result;
                 try
                 {
                     result = end(iar);
                 }
                 catch (Exception exception)
                 {
                     subject.OnError(exception);
                     return;
                 }
                 subject.OnNext(result);
                 subject.OnCompleted();
             }, null);
         }
         catch (Exception exception)
         {
             return Observable.Throw <TResult>(exception, Scheduler.DefaultSchedulers.AsyncConversions);
         }
         return subject.AsObservable();
     });
 }
        public static Func <IObservable <T> > ToAsync <T>(Func <T> function, IScheduler scheduler)
        {
            return(() =>
            {
                var subject = new AsyncSubject <T>();

                scheduler.Schedule(() =>
                {
                    var result = default(T);
                    try
                    {
                        result = function();
                    }
                    catch (Exception exception)
                    {
                        subject.OnError(exception);
                        return;
                    }
                    subject.OnNext(result);
                    subject.OnCompleted();
                });

                return subject.AsObservable();
            });
        }
        public static Func <IObservable <Unit> > ToAsync(Action action, IScheduler scheduler)
        {
            return(() =>
            {
                var subject = new AsyncSubject <Unit>();

                scheduler.Schedule(() =>
                {
                    try
                    {
                        action();
                    }
                    catch (Exception exception)
                    {
                        subject.OnError(exception);
                        return;
                    }
                    subject.OnNext(Unit.Default);
                    subject.OnCompleted();
                });

                return subject.AsObservable();
            });
        }