Table of contents
Collapse the table of content
Expand the table of content

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

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

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


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.


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.


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


Result: 6
Result: one two three


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

Collections.Map Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2020 Microsoft