The List.map function creates a new collection by
applying a function to the given collection.
Just have a look at the below example,
When you print r1 you should get output like 2,3,4,5
let data = [1;2;3;4]
let functiontomap r = r+ 1
let r1 = List.map functiontomap data
printfn "updated data = %A" r1
open System
printfn "Press any key to continue"
Console.ReadKey(true);
Expected output
In previous example, I explicitly defined the input function, however, it is not
required, you can use List.map as below:
In the above way of using List.map, we are directly applying a function to the input
collection.
let data = [1..10]
let resultincrement = data |> List.map (fun x -> x + 1)
let resultsquare = data |> List.map (fun x -> x*x)
printfn "incremented data = %A" resultincrement
printfn "squared data = %A" resultsquare
open System
printfn "Press any key to continue"
Console.ReadKey(true);
Expected output
If you want to covert the above integer collection to strings, that also is very much
possible in a single line statement, like below:
As the input function you need to pass a string
let data = [1..10]
let stringdata = data |> List.map string
printfn "String data = %A" stringdata
open System
printfn "Press any key to continue"
Console.ReadKey(true);
Expected output
I hope this post was useful. Thanks for reading
If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode
or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If
you want to see post on a particular topic please do write on FB page or tweet
me about that, I would love to help you.