Compartir a través de


List.map<'T,'U> Function (F#)

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

Parameters

  • mapping
    Type: 'T -> 'U

    The function to transform elements from the input list.

  • list
    Type: 'T list

    The input list.

Return Value

The list of transformed elements.

Remarks

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

Example

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"

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

Microsoft.FSharp.Collections Namespace (F#)