Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient conversion from String[] to F# List

I'm coming to F# from a C# background and a little behind on the different lists and collections. I recently ran into a case where I needed to go from a string[] to 'T list. I ended up using list comprehension to do the cast:

let lines = File.ReadAllLines(@"C:\LinesOText.txt") // returns a string array
let listOLines = [for l in lines -> l] // use list comprehension to get the f# list

Is there a more efficient way of doing the conversion?

like image 804
Joshua Belden Avatar asked Jul 06 '12 18:07

Joshua Belden


People also ask

Can we convert string [] to string?

So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.

Is Atoi fast?

Atoi is the fastest I could come up with. I compiled with msvc 2010 so it might be possible to combine both templates.


3 Answers

Use List.ofArray or Array.toList.

like image 174
pad Avatar answered Oct 03 '22 23:10

pad


this should do it:

let lines = File.ReadAllLines(@"C:\LinesOText.txt") |> List.ofArray
like image 31
Joshua Avatar answered Oct 03 '22 23:10

Joshua


Here's another way to do it:

let listOfLines = [yield! File.ReadAllLines(@"C:\LinesOText.txt")]
like image 28
Daniel Avatar answered Oct 03 '22 22:10

Daniel