简体   繁体   中英

How do I create an observable function in F#, based on an existing C# snippet?

I am trying to convert the following code to F#:

static void Main(string[] args)
{
    var y = Observable.Create<int>(x =>
        {
            x.OnNext(5);
            return (() => { });
        });

    y.Subscribe(x => Console.WriteLine(x));
}

tried the following :

let ob = Observable.Create<int>(fun x -> 
                                    x.OnNext(5)
                                    fun unit -> unit)  

but no success. What should I be doing?

let observable = Observable.Create(fun (x: IObserver<_>) ->
  x.OnNext(5)
  Action(ignore))

observable.Subscribe(printfn "%d") |> ignore

If your Observable cannot be cancelled (usually when it is synchronous) , it's more efficient to use the singleton Disposable.Empty

Observable.Create(fun (o : IObserver<_>) -> 
    o.OnNext(5)
    Disposable.Empty
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM