Through composition in my C# code I've ended up with a type
IObservable<Maybe<IObservable<T>>> events;
where Maybe is the option monad and IObservable is the reactive monad.
Now I want to transform this into
IObservable<Maybe<T>> flatEvents;
which I think expresses pretty much the same thing. Note I want to preserve the "nothing to see" event embodied by the maybe. I don't want to flatten it out completely so that I only see events when an instance of T is available.
After a bit of trial and error I found I can do the transformation via
var flatEvents = events
.Select(p=>p.Select(q=>q.Select(v=>v.ToMaybe()))
.Else(Observable.Return(None<T>.Default)))
.Switch();
where None<T>.Default
returns a Maybe<T>
which is empty.
T Maybe<T>.Else(T v)
extracts the contained value in the option monad and if there is not one then provides an alternative.
Considering that naming things is the hardest thing is CS I'm looking for a name for this flattening operator. I'm not a haskell programmer but I'm sure I haven't invented anything here and this is some kind of common projection. Is there a name for this?