List.map function creates a new collection by applying a function to the given collection.
Just have a look on below example,
When you print r1 you should get the output as 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 above way of using List.map, we are directly applying 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 above integer collection as string , that also very much possible in single line of statement as below,
As input function you need to pass 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.
Follow @debug_mode