Udostępnij za pośrednictwem


Array.filter<'T> Function (F#)

Returns a new collection containing only the elements of the collection for which the given predicate returns true.

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

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

// Signature:
Array.filter : ('T -> bool) -> 'T [] -> 'T []

// Usage:
Array.filter predicate array

Parameters

  • predicate
    Type: 'T -> bool

    The function to test the input elements.

  • array
    Type: 'T []

    The input array.

Return Value

An array containing the elements for which the given predicate returns true.

Remarks

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

Example

The following example shows how to use Array.filter to select elements from an array.

let names = [|"Bob"; "Ann"; "Stephen"; "Vivek"; "Fred"; "Kim"; "Brian"; "Ling"; "Jane"; "Jonathan"|]
let longNames = names |> Array.filter (fun x -> x.Length > 4)

printfn "names = %A\n" names
printfn "longNames = %A" longNames
names = [|"Bob"; "Ann"; "Stephen"; "Vivek"; "Fred"; "Kim"; "Brian"; "Ling"; "Jane"; "Jonathan"|]

longNames = [|"Stephen"; "Vivek"; "Brian"; "Jonathan"|]

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.Array Module (F#)

Microsoft.FSharp.Collections Namespace (F#)