Udostępnij za pośrednictwem


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

Searches the map looking for the first element where the given function returns a Some value.

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

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

// Signature:
Map.tryPick : ('Key -> 'T -> 'U option) -> Map<'Key,'T> -> 'U option (requires comparison)

// Usage:
Map.tryPick chooser table

Parameters

  • chooser
    Type: 'Key -> 'T -> 'U option

    The function to generate options from the key/value pairs.

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

    The input map.

Return Value

The first result.

Remarks

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

Example

The following code shows the use of the Map.tryPick function.

let map1 = [ for i in 1 .. 100 -> (i, 100 - i) ] |> Map.ofList
let result = Map.tryPick (fun key value -> if key = value then Some(key) else None) map1
match result with
| Some x -> printfn "Result where key and value are the same: %d" x
| None -> printfn "No result satisifies the condition."

Output

Result where key and value are the same: 50

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#)