How do I change the Rx Builder implementation to fix the stack overflow exception?

前端 未结 5 983
醉话见心
醉话见心 2021-02-20 02:50

I\'m trying to come up with an Rx Builder to use Reactive Extension within the F# Computation Expression syntax. How do I fix it so that it doesnt blow the stack? Like the Seq e

5条回答
  •  梦如初夏
    2021-02-20 03:16

    If we remove the syntactic sugar from this computation expression (aka Monad) we will have:

    let rec g x = Observable.Defer (fun () -> Observable.merge(Observable.Return x, g (x + 1) )
    

    Or in C#:

    public static IObservable g(int x)
    {
        return Observable.Defer(() =>
        {
          return Observable.Merge(Observable.Return(x), g(x + 1));                    
        });
    }
    

    Which is definitely not tail recursive. I think if you can make it tail recursive then it would probably solve your problem

提交回复
热议问题