19 people like it.

F# impl of SelectMany and TakeUntil from Rx

F# implementation of SelectMany and TakeUntil (from Rx) on F# events

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
open System
open System.Windows.Forms

let frm = new Form()

let selectMany (f:'T -> IEvent<'Del,'T>) (src:IEvent<'Del,'T>) = 
    let customEvent = new Event<'Del,'T>()
    src
    |> Event.add (fun e -> f(e) |> Event.add (fun t -> customEvent.Trigger(null,t)))
    customEvent.Publish 

let takeUntil (takeTill:IEvent<'Del,'T>) (src:IEvent<'Del,'T>) = 
    let customEvent = new Event<'Del,'T>()
    let s = src.Subscribe (fun e -> customEvent.Trigger(null,e))
    let z = takeTill.Subscribe(fun e -> s.Dispose())
    customEvent.Publish

frm.MouseDown
|> selectMany (fun e -> takeUntil frm.MouseUp frm.MouseMove)
|> Event.add (fun e -> frm.Text <- e.X.ToString() + " -- " + e.Y.ToString())

frm.ShowDialog() |> ignore
namespace System
namespace System.Windows
namespace System.Windows.Forms
val frm : Form

Full name: Script.frm
Multiple items
type Form =
  inherit ContainerControl
  new : unit -> Form
  member AcceptButton : IButtonControl with get, set
  member Activate : unit -> unit
  member ActiveMdiChild : Form
  member AddOwnedForm : ownedForm:Form -> unit
  member AllowTransparency : bool with get, set
  member AutoScale : bool with get, set
  member AutoScaleBaseSize : Size with get, set
  member AutoScroll : bool with get, set
  member AutoSize : bool with get, set
  ...
  nested type ControlCollection

Full name: System.Windows.Forms.Form

--------------------
Form() : unit
val selectMany : f:('T -> IEvent<'Del,'T>) -> src:IEvent<'Del,'T> -> IEvent<'Del,'T> (requires delegate and 'Del :> Delegate)

Full name: Script.selectMany
val f : ('T -> IEvent<'Del,'T>) (requires delegate and 'Del :> Delegate)
type IEvent<'T> = IEvent<Handler<'T>,'T>

Full name: Microsoft.FSharp.Control.IEvent<_>
val src : IEvent<'Del,'T> (requires delegate and 'Del :> Delegate)
val customEvent : Event<'Del,'T> (requires delegate and 'Del :> Delegate)
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
val add : callback:('T -> unit) -> sourceEvent:IEvent<'Del,'T> -> unit (requires delegate and 'Del :> Delegate)

Full name: Microsoft.FSharp.Control.Event.add
val e : 'T
val t : 'T
member Event.Trigger : sender:obj * args:'Args -> unit
property Event.Publish: IEvent<'Del,'T>
val takeUntil : takeTill:IEvent<'Del,'T> -> src:IEvent<'Del,'T> -> IEvent<'Del,'T> (requires delegate and 'Del :> Delegate)

Full name: Script.takeUntil
val takeTill : IEvent<'Del,'T> (requires delegate and 'Del :> Delegate)
val s : IDisposable
member IObservable.Subscribe : callback:('T -> unit) -> IDisposable
IObservable.Subscribe(observer: IObserver<'T>) : IDisposable
val z : IDisposable
IDisposable.Dispose() : unit
event Control.MouseDown: IEvent<MouseEventHandler,MouseEventArgs>
val e : MouseEventArgs
event Control.MouseUp: IEvent<MouseEventHandler,MouseEventArgs>
event Control.MouseMove: IEvent<MouseEventHandler,MouseEventArgs>
property Form.Text: string
property MouseEventArgs.X: int
Int32.ToString() : string
Int32.ToString(provider: IFormatProvider) : string
Int32.ToString(format: string) : string
Int32.ToString(format: string, provider: IFormatProvider) : string
property MouseEventArgs.Y: int
Form.ShowDialog() : DialogResult
Form.ShowDialog(owner: IWin32Window) : DialogResult
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Raw view Test code New version

More information

Link:http://fssnip.net/1z
Posted:13 years ago
Author:Ankur Dhama
Tags: events , rx