Expand Minimize
This topic has not yet been rated - Rate this topic

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

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

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

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

// Signature:
Array.choose : ('T -> 'U option) -> 'T [] -> 'U []

// Usage:
Array.choose chooser array
chooser

Type: 'T -> 'U option

The function to generate options from the elements.

array

Type: 'T []

The input array.

The array of results.

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 Array.choose to perform an operation only on the even numbers in an array.


printfn "%A" (Array.choose (fun elem -> if elem % 2 = 0 then
                                            Some(float (elem*elem - 1))
                                        else
                                            None) [| 1 .. 10 |])


[|3.0; 15.0; 35.0; 63.0; 99.0|]

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

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.