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

Creates two new maps, one containing the bindings for which the given predicate returns true, and the other the remaining bindings.

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

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

// Signature:
Map.partition : ('Key -> 'T -> bool) -> Map<'Key,'T> -> Map<'Key,'T> * Map<'Key,'T> (requires comparison)

// Usage:
Map.partition predicate table

Parameters

  • predicate
    Type: 'Key -> 'T -> bool

    The function to test the input elements.

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

    The input map.

Return Value

A pair of maps in which the first contains the elements for which the predicate returned true and the second containing the elements for which the predicated returned false.

Remarks

This function is named Partition 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 shows how to use Map.partition.

let map1 = [ for i in 1..10 -> (i, i*i)] |> Map.ofList
let (mapEven, mapOdd) = Map.partition (fun key value -> key % 2 = 0) map1
printfn "Evens: %A" mapEven
printfn "Odds: %A" mapOdd

Output

Evens: map [(2, 4); (4, 16); (6, 36); (8, 64); (10, 100)]
Odds: map [(1, 1); (3, 9); (5, 25); (7, 49); (9, 81)]

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

Collections.Map Module (F#)

Microsoft.FSharp.Collections Namespace (F#)