Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created October 16, 2009 17:52
Show Gist options
  • Save mattpodwysocki/211922 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/211922 to your computer and use it in GitHub Desktop.
module Event =
open System.Threading
let concat (source: IEvent<IEvent<_,_>>) =
let event = new Event<_>()
source.Add (Event.listen event.Trigger)
event.Publish
let collect f =
Event.map f >> concat
let post (context:SynchronizationContext) (sourceEvent: IEvent<IEvent<_,_>>) =
let event = new Event<_>()
sourceEvent.Add(fun args ->
context.Post(SendOrPostCallback(fun _ -> event.Trigger args), null))
event.Publish
let sample interval (sourceEvent:IEvent<'del, 'a>) =
let event = new Event<_>()
let intervalCount = ref interval
sourceEvent.Add(fun args ->
if !intervalCount > 0 then decr intervalCount
else intervalCount := interval; event.Trigger args)
event.Publish
let skip n (sourceEvent:IEvent<_,_>) =
let event = new Event<_>()
let remaining = ref n
sourceEvent.Add(fun args -> if !remaining > 0 then decr remaining else event.Trigger args)
event.Publish
let skipWhile predicate (sourceEvent:IEvent<_,_>) =
let event = new Event<_>()
let ok = ref true
sourceEvent.Add(fun args ->
if !ok then
ok := predicate args
else
event.Trigger args)
event.Publish
let take n (sourceEvent:IEvent<_,_>) =
let event = new Event<_>()
let remaining = ref n
sourceEvent.Add(fun args -> if !remaining > 0 then decr remaining; event.Trigger args)
event.Publish
let takeWhile predicate (sourceEvent:IEvent<_,_>) =
let event = new Event<_>()
let ok = ref true
sourceEvent.Add(fun args ->
if !ok then
ok := predicate args
event.Trigger args)
event.Publish
let until (other: IEvent<_,_>) (source: IEvent<_,_>) =
let event = new Event<_>()
let ok = ref true
other .Add(fun _ -> ok := false)
source.Add(fun args -> if !ok then event.Trigger args)
event.Publish
let waitUntil (other: IEvent<_,_>) (source: IEvent<_,_>) =
let event = new Event<_>()
let ok = ref false
other .Add(fun _ -> ok := true)
source.Add(fun args -> if !ok then event.Trigger args)
event.Publish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment