6 people like it.

Simple Reactive Extensions Example

Subject is a class that implements both IObservable<'T> and IObserver<'T>

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
// Rx 1.0:
//#r "System.CoreEx" //for interactive and scripts
//#r "System.Reactive.dll" 
//open System
//open System.Collections.Generic

//Rx 2.1:
#r "System.ComponentModel.Composition.dll"
#r "../packages/Rx-Interfaces.2.1.30214.0/lib/Net45/System.Reactive.Interfaces.dll"
#r "../packages/Rx-Core.2.1.30214.0/lib/Net45/System.Reactive.Core.dll"
#r "../packages/Rx-Linq.2.1.30214.0/lib/Net45/System.Reactive.Linq.dll"
open System.Reactive.Subjects

// ---

let observable = new Subject<int>()

let mySubscribe =
    let interested = observable |> Observable.filter (fun x -> x%2=0)
    interested.Subscribe(fun i -> Console.WriteLine("Hello " + i.ToString()))

let myYields =
    observable.OnNext(1)
    observable.OnNext(2)
    observable.OnNext(3)
    observable.OnNext(4)
namespace System
namespace System.Reactive
namespace System.Reactive.Subjects
val observable : Subject<int>

Full name: Script.observable
Multiple items
type Subject =
  static member Create<'TSource, 'TResult> : observer:IObserver<'TSource> * observable:IObservable<'TResult> -> ISubject<'TSource, 'TResult>
  static member Synchronize<'TSource, 'TResult> : subject:ISubject<'TSource, 'TResult> -> ISubject<'TSource, 'TResult> + 1 overload

Full name: System.Reactive.Subjects.Subject

--------------------
type Subject<'T> =
  new : unit -> Subject<'T>
  member Dispose : unit -> unit
  member HasObservers : bool
  member OnCompleted : unit -> unit
  member OnError : error:Exception -> unit
  member OnNext : value:'T -> unit
  member Subscribe : observer:IObserver<'T> -> IDisposable

Full name: System.Reactive.Subjects.Subject<_>

--------------------
Subject() : unit
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val mySubscribe : System.IDisposable

Full name: Script.mySubscribe
val interested : System.IObservable<int>
module Observable

from Microsoft.FSharp.Control
val filter : predicate:('T -> bool) -> source:System.IObservable<'T> -> System.IObservable<'T>

Full name: Microsoft.FSharp.Control.Observable.filter
val x : int
member System.IObservable.Subscribe : callback:('T -> unit) -> System.IDisposable
System.IObservable.Subscribe(observer: System.IObserver<int>) : System.IDisposable
val i : int
System.Int32.ToString() : string
System.Int32.ToString(provider: System.IFormatProvider) : string
System.Int32.ToString(format: string) : string
System.Int32.ToString(format: string, provider: System.IFormatProvider) : string
val myYields : unit

Full name: Script.myYields
Subject.OnNext(value: int) : unit

More information

Link:http://fssnip.net/8E
Posted:13 years ago
Author:Tuomas Hietanen
Tags: rx , reactive