Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

List.choose<'T,'U> Function (F#)

Applies the given function f to each element x of the list. Returns the list comprised of the results for each element where the function returns Some(f(x)).

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

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

// Signature:
List.choose : ('T -> 'U option) -> 'T list -> 'U list

// Usage:
List.choose chooser list
chooser

Type: 'T -> 'U option

The function to generate options from the elements.

list

Type: 'T list

The input list.

The list comprising the values selected from the chooser function.

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

The following code demonstrates the use of List.choose to select capitalized words out of a list of words.

let listWords = [ "and"; "Rome"; "Bob"; "apple"; "zebra" ]
let isCapitalized (string1:string) = System.Char.IsUpper string1.[0]
let results = List.choose (fun elem ->
    match elem with
    | elem when isCapitalized elem -> Some(elem + "'s")
    | _ -> None) listWords
printfn "%A" results

Output

["Rome's"; "Bob's"]

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

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.