Share via


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

Microsoft.FSharp.Collections Namespace (F#)