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: 
//#r "System.CoreEx" //for interactive and scripts
//#r "System.Reactive.dll" 
open System
open System.Collections.Generic

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.Collections
namespace System.Collections.Generic
val observable : IObservable<int>

Full name: Script.observable
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 : IDisposable

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

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

Full name: Microsoft.FSharp.Control.Observable.filter
val x : int
(extension) IObservable.Subscribe<'T>() : IDisposable
   (+0 other overloads)
member IObservable.Subscribe : callback:('T -> unit) -> IDisposable
   (+0 other overloads)
(extension) IObservable.Subscribe<'T>(onNext: Action<'T>) : IDisposable
   (+0 other overloads)
(extension) IObservable.Subscribe<'T>(token: Threading.CancellationToken) : unit
   (+0 other overloads)
IObservable.Subscribe(observer: IObserver<int>) : IDisposable
   (+0 other overloads)
(extension) IObservable.Subscribe<'T>(onNext: Action<'T>, onError: Action<exn>) : IDisposable
   (+0 other overloads)
(extension) IObservable.Subscribe<'T>(onNext: Action<'T>, onCompleted: Action) : IDisposable
   (+0 other overloads)
(extension) IObservable.Subscribe<'T>(observer: IObserver<'T>, token: Threading.CancellationToken) : unit
   (+0 other overloads)
(extension) IObservable.Subscribe<'T>(onNext: Action<'T>, token: Threading.CancellationToken) : unit
   (+0 other overloads)
(extension) IObservable.Subscribe<'T>(onNext: Action<'T>, onError: Action<exn>, onCompleted: Action) : IDisposable
   (+0 other overloads)
val i : int
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.WriteLine() : unit
   (+0 other overloads)
Console.WriteLine(value: string) : unit
   (+0 other overloads)
Console.WriteLine(value: obj) : unit
   (+0 other overloads)
Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
Console.WriteLine(value: int64) : unit
   (+0 other overloads)
Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
Console.WriteLine(value: int) : unit
   (+0 other overloads)
Console.WriteLine(value: float32) : unit
   (+0 other overloads)
Console.WriteLine(value: float) : unit
   (+0 other overloads)
Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
Int32.ToString() : string
Int32.ToString(provider: IFormatProvider) : string
Int32.ToString(format: string) : string
Int32.ToString(format: string, provider: IFormatProvider) : string
val myYields : obj

Full name: Script.myYields
Next Version Raw view Test code New version

More information

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