FSharpPlus icon indicating copy to clipboard operation
FSharpPlus copied to clipboard

Is it possible to add concrete implementation of monad for IObservable<'a> ?

Open Hong-Xiang opened this issue 4 years ago • 1 comments

Currently IObservable<'a> works fine with map / |>>, but does not work with monad CE.

Since IObservable<'a> has LINQ extension methods IObservable.Select, IObservable.SelectMany and Observable.Return. It seems that it might be nature to have monad instance just following System.Reactive.Linq 's implementation.

As discussed in here, adding concrete implementation for IObservable<'a> in FSharpPlu.Control might be the easiest way to achieve this.

Is it possible to add monad instance concrete implementation directly to F#+?

If there is no other constrains, adding concreate implementation in Control/Monad.fs might be as easy as several lines of code to direct call IObservable<'a>'s corresponding extension methods.

But there might be some problems, like introduce dependency to System.Reactive, and maybe Using / Defer and other methods needs also be considered. But considering IObservable<'a> is already provided in System namespace, and Rx.Net is an popular functional style library, solving these problems might have enough benefits.

Hong-Xiang avatar Jul 10 '21 08:07 Hong-Xiang

But there might be some problems, like introduce dependency to System.Reactive,

I think that's the main concern.

We could add an implementation that doesn't take a dependency on System.Reactive, but I'm not very familiar with the details of different implementations of SelectMany.

In anycase, it should always be possible to "extract" the SelectMany code from System.Reactive (since it's open source) and put it here in an Observable.fs extension file, then add the corresponding overload for >>=.

If you want to go that road that contribution would be more than welcome and I can certainly help you integrating it.

gusty avatar Jul 10 '21 15:07 gusty