List.filter<'T> Function (F#)
Visual Studio 2012
Returns a new collection containing only the elements of the collection for which the given predicate returns true.
Namespace/Module Path: Microsoft.FSharp.Collections.List
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
List.filter : ('T -> bool) -> 'T list -> 'T list
// Usage:
List.filter predicate list
The following example demonstrates the use of List.filter.
let evenOnlyList = List.filter (fun x -> x % 2 = 0) [1; 2; 3; 4; 5; 6]
The resulting list is [2; 4; 6].
The following example shows another typical use for List.filter.
let data = [("Cats",4); ("Dogs",5); ("Mice",3); ("Elephants",2)] let res = data |> List.filter (fun (nm,x) -> nm.Length <= 4) printfn "Animals with short names: %A" res
Animals with short names: [("Cats", 4); ("Dogs", 5); ("Mice", 3)]