Map.fold<'Key,'T,'State> Function (F#)

Folds over the bindings in the map

Namespace/Module Path: Microsoft.FSharp.Collections.Map

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

// Signature:
Map.fold : ('State -> 'Key -> 'T -> 'State) -> 'State -> Map<'Key,'T> -> 'State (requires comparison)

// Usage:
Map.fold folder state table

Parameters

  • folder
    Type: 'State -> 'Key -> 'T -> 'State

    The function to update the state given the input key/value pairs.

  • state
    Type: 'State

    The initial state.

  • table
    Type: Map<'Key,'T>

    The input map.

Return Value

The final state value.

Remarks

This function is named Fold 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 illustrates the use of Map.fold.

let map1 = Map.ofList [ (1, "one"); (2, "two"); (3, "three") ]
// Sum the keys.
let result1 = Map.fold (fun state key value -> state + key) 0 map1
printfn "Result: %d" result1
// Concatenate the values.
let result2 = Map.fold (fun state key value -> state + value + " ") "" map1
printfn "Result: %s" result2

Output

Result: 6
Result: one two three 

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Collections.Map Module (F#)

Microsoft.FSharp.Collections Namespace (F#)