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

前端 未结 5 981
醉话见心
醉话见心 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:17

    What about something like this?

    type rxBuilder() =    
       member this.Delay (f : unit -> 'a IObservable) = 
                   { new IObservable<_> with
                        member this.Subscribe obv = (f()).Subscribe obv }
       member this.Combine (xs:'a IObservable, ys: 'a IObservable) =
                   { new IObservable<_> with
                        member this.Subscribe obv = xs.Subscribe obv ; 
                                                    ys.Subscribe obv }
       member this.Yield x = Observable.Return x
       member this.YieldFrom xs = xs
    
    let rx = rxBuilder()
    
    let rec f x = rx { yield x 
                       yield! f (x + 1) }
    
    do f 5 |> Observable.subscribe (fun x -> Console.WriteLine x) |> ignore
    
    do System.Console.ReadLine() |> ignore
    

    http://rxbuilder.codeplex.com/ (created for the purpose of experimenting with RxBuilder)

    The xs disposable is not wired up. As soon as I try to wire up the disposable it goes back to blowing up the stack.

提交回复
热议问题