3

There's a nice F# workflow builder for Rx here:

http://blogs.msdn.com/b/dsyme/archive/2011/05/30/nice-f-syntax-for-rx-reactive-extensions.aspx

I've been trying to make a Using implementation for the workflow but keep banging my head against the wall. Maybe it's too late here.

How would one create this?

type RxBuilder () =

    // ...

    member this.Using (disposable, f) =
        Observable.Using(???)

Thanks in advance.

Bent Rasmussen
  • 5,538
  • 9
  • 44
  • 63

1 Answers1

4

Firstly, the original code needs to be updated slightly for the latest Rx releases. Namely, For and While should be implemented as:

member this.For (xs : 'a seq, f: 'a -> 'b IObservable) =
    Observable.SelectMany(xs.ToObservable(), new Func<_, IObservable<_>>(f)) 

member this.While (f, xs: 'a IObservable) =
    Observable.TakeWhile (xs, new Func<_,bool>(fun _ -> f()))

Then, based on this, you can use the following for an implementation of Using:

member this.Using(res:#IDisposable, body) = this.TryFinally(body res, fun () -> match res with null -> () | disp -> disp.Dispose())
yamen
  • 15,390
  • 3
  • 42
  • 52
  • Nice! I like the constraint notation. It looks like one should take a closer look at fsharpx. – Bent Rasmussen Jun 18 '12 at 03:06
  • My implementation of For actually looked like this: member this.For (xs : 'a IObservable, f: 'a -> 'b IObservable) : 'b IObservable = xs.SelectMany(f). Not tested yet tho. – Bent Rasmussen Jun 18 '12 at 03:09
  • That's basically the same thing, I just didn't use the extension method format for style reasons (to match the other implementations), and the explicit type arguments in order to confirm everything worked as expected. – yamen Jun 18 '12 at 04:48