Share via


Event.scan<'U,'T,'Del> Function (F#)

Returns a new event that consists of the results of applying the given accumulating function to successive values triggered on the input event.

Namespace/Module Path: Microsoft.FSharp.Control.Event

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
Event.scan : ('U -> 'T -> 'U) -> 'U -> IEvent<'Del,'T> -> IEvent<'U> (requires delegate)

// Usage:
Event.scan collector state sourceEvent

Parameters

  • collector
    Type: 'U -> 'T -> 'U

    The function to update the state with each event value.

  • state
    Type: 'U

    The initial state.

  • sourceEvent
    Type: IEvent<'Del,'T>

    The input event.

Return Value

An event that fires on the updated state values.

Remarks

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 is not triggered by multiple threads simultaneously.

This function is named Scan in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following code example shows how to use the Event.scan function. This code implements a simple click counter. Every time the user clicks on the form, the state increments by 1 and the form's text is changed to display the new state.

// This code implements a simple click counter. Every time 
// the user clicks the form, the state increments by 1 
// and the form's text is changed to display the new state. 

open System.Windows.Forms
open System.Drawing
open Microsoft.FSharp.Core

let form = new Form(Text = "F# Windows Form",
                    Visible = true,
                    TopMost = true)

let initialState = 0

form.Click
|> Event.scan (fun state _ -> state + 1) initialState
|> Event.add (fun state -> form.Text <- state.ToString() )

Platforms

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Reference

Control.Event Module (F#)

Microsoft.FSharp.Control Namespace (F#)