FSharp.Core


Event Module

Contains operations for working with values of type IEvent.

Functions and values

Function or value Description

Event.add callback sourceEvent

Full Usage: Event.add callback sourceEvent

Parameters:
    callback : 'T -> unit - The function to call when the event is triggered.
    sourceEvent : IEvent<'Del, 'T> - The input event.

Runs the given function each time the given event is triggered.

callback : 'T -> unit

The function to call when the event is triggered.

sourceEvent : IEvent<'Del, 'T>

The input event.

Example

 open System
 
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed
 
 let timer = createTimer 1000
 
 Event.add (fun (event: Timers.ElapsedEventArgs) -> printfn $"{event.SignalTime} ")  timer
 
 Console.ReadLine() |> ignore
The sample will output the timer event every second: 2/14/2022 11:52:05 PM 2/14/2022 11:52:06 PM 2/14/2022 11:52:07 PM 2/14/2022 11:52:08 PM

Event.choose chooser sourceEvent

Full Usage: Event.choose chooser sourceEvent

Parameters:
    chooser : 'T -> 'U option - The function to select and transform event values to pass on.
    sourceEvent : IEvent<'Del, 'T> - The input event.

Returns: IEvent<'U> An event that fires only when the chooser returns Some.

Returns a new event which fires on a selection of messages from the original event. The selection function takes an original message to an optional new message.

chooser : 'T -> 'U option

The function to select and transform event values to pass on.

sourceEvent : IEvent<'Del, 'T>

The input event.

Returns: IEvent<'U>

An event that fires only when the chooser returns Some.

Example

 open System
 
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed
 
 let timer = createTimer 1000
 
 let getEvenSeconds (number: Timers.ElapsedEventArgs) =
     match number with
     | _ when number.SignalTime.Second % 2 = 0 -> Some number.SignalTime
     | _ -> None
 
 let evenSecondsEvent = Event.choose getEvenSeconds timer
 
 evenSecondsEvent.Subscribe(fun x -> printfn $"{x} ")
 |> ignore
 
 Console.ReadLine() |> ignore
The sample will output: 2/15/2022 12:04:04 AM 2/15/2022 12:04:06 AM 2/15/2022 12:04:08 AM

Event.filter predicate sourceEvent

Full Usage: Event.filter predicate sourceEvent

Parameters:
    predicate : 'T -> bool - The function to determine which triggers from the event to propagate.
    sourceEvent : IEvent<'Del, 'T> - The input event.

Returns: IEvent<'T> An event that only passes values that pass the predicate.

Returns a new event that listens to the original event and triggers the resulting event only when the argument to the event passes the given function.

predicate : 'T -> bool

The function to determine which triggers from the event to propagate.

sourceEvent : IEvent<'Del, 'T>

The input event.

Returns: IEvent<'T>

An event that only passes values that pass the predicate.

Example

 open System
 
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed
 
 let timer = createTimer 1000
 
 let getEvenSeconds (number: Timers.ElapsedEventArgs) =
     match number with
     | _ when number.SignalTime.Second % 2 = 0 -> true
     | _ -> false
 
 let evenSecondsEvent = Event.filter getEvenSeconds timer
 
 evenSecondsEvent.Subscribe(fun x -> printfn $"{x} ")
 |> ignore
 
 Console.ReadLine() |> ignore
The sample will only output even seconds: 2/15/2022 12:03:08 AM 2/15/2022 12:03:10 AM 2/15/2022 12:03:12 AM 2/15/2022 12:03:14 AM

Event.map mapping sourceEvent

Full Usage: Event.map mapping sourceEvent

Parameters:
    mapping : 'T -> 'U - The function to transform event values.
    sourceEvent : IEvent<'Del, 'T> - The input event.

Returns: IEvent<'U> An event that passes the transformed values.

Returns a new event that passes values transformed by the given function.

mapping : 'T -> 'U

The function to transform event values.

sourceEvent : IEvent<'Del, 'T>

The input event.

Returns: IEvent<'U>

An event that passes the transformed values.

Example

 open System
 
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed
 
 let timer = createTimer 1000
 
 let transformSeconds (number: Timers.ElapsedEventArgs) =
     match number with
     | _ when number.SignalTime.Second % 2 = 0 -> 100
     | _ -> -500
 
 let evenSecondsEvent = Event.map transformSeconds timer
 
 evenSecondsEvent.Subscribe(fun x -> printf $"{x} ")
 |> ignore
 
 Console.ReadLine() |> ignore
The sample will transform the seconds if it's even or odd number and the output is: -500 100 -500 100 -500 100

Event.merge event1 event2

Full Usage: Event.merge event1 event2

Parameters:
    event1 : IEvent<'Del1, 'T> - The first input event.
    event2 : IEvent<'Del2, 'T> - The second input event.

Returns: IEvent<'T> An event that fires when either of the input events fire.

Fires the output event when either of the input events fire.

event1 : IEvent<'Del1, 'T>

The first input event.

event2 : IEvent<'Del2, 'T>

The second input event.

Returns: IEvent<'T>

An event that fires when either of the input events fire.

Example

 open System.Reactive.Linq
 open System
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed

 let oneSecondTimer = createTimer 1000
 let fiveSecondsTimer = createTimer 5000

 let result = Event.merge oneSecondTimer fiveSecondsTimer

 result.Subscribe(fun output -> printfn $"Output - {output.SignalTime} ")
 |> ignore

 Console.ReadLine() |> ignore
The sample will output: Output - 2/15/2022 12:10:40 AM Output - 2/15/2022 12:10:41 AM Output - 2/15/2022 12:10:41 AM Output - 2/15/2022 12:10:42 AM Output - 2/15/2022 12:10:43 AM

Event.pairwise sourceEvent

Full Usage: Event.pairwise sourceEvent

Parameters:
    sourceEvent : IEvent<'Del, 'T> - The input event.

Returns: IEvent<'T * 'T> An event that triggers on pairs of consecutive values passed from the source event.

Returns a new event that triggers on the second and subsequent triggerings of the input event. The Nth triggering of the input event passes the arguments from the N-1th and Nth triggering as a pair. The argument passed to the N-1th triggering is held in hidden internal state until the Nth triggering occurs.

sourceEvent : IEvent<'Del, 'T>

The input event.

Returns: IEvent<'T * 'T>

An event that triggers on pairs of consecutive values passed from the source event.

Example

 open System
 
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed
 
 let timer = createTimer 1000
 
 let pairWise = Event.pairwise timer
 
 let extractPair (pair: Timers.ElapsedEventArgs * Timers.ElapsedEventArgs) =
     let leftPair, rightPair = pair
     printfn $"(Left): {leftPair.SignalTime} (Right): {rightPair.SignalTime}"
 
 pairWise.Subscribe(extractPair) |> ignore
 
 Console.ReadLine() |> ignore
The sample will output the timer event every second: (Left): 2/14/2022 11:58:46 PM (Right): 2/14/2022 11:58:46 PM (Left): 2/14/2022 11:58:46 PM (Right): 2/14/2022 11:58:47 PM (Left): 2/14/2022 11:58:47 PM (Right): 2/14/2022 11:58:48 PM

Event.partition predicate sourceEvent

Full Usage: Event.partition predicate sourceEvent

Parameters:
    predicate : 'T -> bool - The function to determine which output event to trigger.
    sourceEvent : IEvent<'Del, 'T> - The input event.

Returns: IEvent<'T> * IEvent<'T> A tuple of events. The first is triggered when the predicate evaluates to true and the second when the predicate evaluates to false.

Returns a new event that listens to the original event and triggers the first resulting event if the application of the predicate to the event arguments returned true, and the second event if it returned false.

predicate : 'T -> bool

The function to determine which output event to trigger.

sourceEvent : IEvent<'Del, 'T>

The input event.

Returns: IEvent<'T> * IEvent<'T>

A tuple of events. The first is triggered when the predicate evaluates to true and the second when the predicate evaluates to false.

Example

 open System
 
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed
 
 let timer = createTimer 1000
 
 let getEvenSeconds (number: Timers.ElapsedEventArgs) =
     match number with
     | _ when number.SignalTime.Second % 2 = 0 -> true
     | _ -> false
 
 let leftPartition, rightPartition = Event.partition getEvenSeconds timer
 
 leftPartition.Subscribe(fun x -> printfn $"Left partition: {x.SignalTime}")
 |> ignore
 
 rightPartition.Subscribe(fun x -> printfn $"Right partition: {x.SignalTime}")
 |> ignore
 
 Console.ReadLine() |> ignore
The sample will partition into two events if it is even or odd seconds: Right partition: 2/15/2022 12:00:27 AM Left partition: 2/15/2022 12:00:28 AM Right partition: 2/15/2022 12:00:29 AM Left partition: 2/15/2022 12:00:30 AM Right partition: 2/15/2022 12:00:31 AM

Event.scan collector state sourceEvent

Full Usage: Event.scan collector state sourceEvent

Parameters:
    collector : 'U -> 'T -> 'U - The function to update the state with each event value.
    state : 'U - The initial state.
    sourceEvent : IEvent<'Del, 'T> - The input event.

Returns: IEvent<'U> An event that fires on the updated state values.

Returns a new event consisting of the results of applying the given accumulating function to successive values triggered on the input event. An item of internal state records the current value of the state parameter. The internal state is not locked during the execution of the accumulation function, so care should be taken that the input IEvent not triggered by multiple threads simultaneously.

collector : 'U -> 'T -> 'U

The function to update the state with each event value.

state : 'U

The initial state.

sourceEvent : IEvent<'Del, 'T>

The input event.

Returns: IEvent<'U>

An event that fires on the updated state values.

Example

 open System
 
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed
 
 let timer = createTimer 1000
 
 let multiplyBy number =
     fun (timerEvent: Timers.ElapsedEventArgs) -> number * timerEvent.SignalTime./// Second
 
 let initialState = 2
 
 let scan = Event.scan multiplyBy initialState timer
 
 scan.Subscribe(fun x -> printf "%A " x) |> ignore
 
 Console.ReadLine() |> ignore
The sample will output depending on your timestamp. It will multiply the seconds with an initial state of 2: 106 5724 314820 17629920 1004905440 -1845026624 -1482388416

Event.split splitter sourceEvent

Full Usage: Event.split splitter sourceEvent

Parameters:
    splitter : 'T -> Choice<'U1, 'U2> - The function to transform event values into one of two types.
    sourceEvent : IEvent<'Del, 'T> - The input event.

Returns: IEvent<'U1> * IEvent<'U2> A tuple of events. The first fires whenever splitter evaluates to Choice1of1 and the second fires whenever splitter evaluates to Choice2of2.

Returns a new event that listens to the original event and triggers the first resulting event if the application of the function to the event arguments returned a Choice1Of2, and the second event if it returns a Choice2Of2.

splitter : 'T -> Choice<'U1, 'U2>

The function to transform event values into one of two types.

sourceEvent : IEvent<'Del, 'T>

The input event.

Returns: IEvent<'U1> * IEvent<'U2>

A tuple of events. The first fires whenever splitter evaluates to Choice1of1 and the second fires whenever splitter evaluates to Choice2of2.

Example

 open System
 
 let createTimer interval =
     let timer = new Timers.Timer(interval)
     timer.AutoReset <- true
     timer.Enabled <- true
     timer.Elapsed
 
 let timer = createTimer 1000
 
 let bySeconds (timerEvent: Timers.ElapsedEventArgs) =
     match timerEvent.SignalTime.Second % 2 = 0 with
     | true -> Choice1Of2 timerEvent.SignalTime.Second
     | false -> Choice2Of2 $"{timerEvent.SignalTime.Second} is not an even num ber"
 
 let evenSplit, printOddNumbers = Event.split bySeconds timer
 
 let printOutput event functionName =
     Event.add (fun output -> printfn $"{functionName} - Split output: {output}. /// Type: {output.GetType()}") event
 
 printOutput evenSplit (nameof evenSplit) |> ignore
 
 printOutput printOddNumbers (nameof printOddNumbers)
 |> ignore
 
 Console.ReadLine() |> ignore
The sample will split the events by even or odd seconds: evenSplit - Split output: 44. Type: System.Int32 printOddNumbers - Split output: 45 is not an even number. Type: System.String evenSplit - Split output: 46. Type: System.Int32 printOddNumbers - Split output: 47 is not an even number. Type: System.String evenSplit - Split output: 48. Type: System.Int32 printOddNumbers - Split output: 49 is not an even number. Type: System.String