List.map<'T,'U> Function (F#)
Visual Studio 2012
Creates a new collection whose elements are the results of applying the given function to each of the elements of the collection.
Namespace/Module Path: Microsoft.FSharp.Collections.List
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
List.map : ('T -> 'U) -> 'T list -> 'U list
// Usage:
List.map mapping list
The following example demonstrates the use of List.map.
let data = [1;2;3;4] let r1 = data |> List.map (fun x -> x + 1) printfn "Adding '1' using map = %A" r1 let r2 = data |> List.map string printfn "Converting to strings using map = %A" r2 let r3 = data |> List.map (fun x -> (x,x)) printfn "Tupling up using map = %A" r3
Adding '1' using map = [2; 3; 4; 5] Converting to strings using map = ["1"; "2"; "3"; "4"] Tupling up using map = [(1, 1); (2, 2); (3, 3); (4, 4)]
The next example demonstrates the use of List.map to transform data into a different format.
let data = [(1,1,2001); (2,2,2004); (6,17,2009)] let list1 = data |> List.map (fun (a,b,c) -> let date = new System.DateTime(c, a, b) date.ToString("F")) for i in list1 do printfn "%A" i
"Monday, January 01, 2001 12:00:00 AM" "Monday, February 02, 2004 12:00:00 AM" "Wednesday, June 17, 2009 12:00:00 AM"