Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Array.map omitting the first element of the array in F#

Tags:

f#

I have just started playing with F#, so this question will probably be quite basic...

I would like to read a text file line by line, and then ignore the first line and process the other lines using a given function. So I was thinking of using something along the lines of:

File.ReadAllLines(path)
|> Array.(ignore first element)
|> Array.map processLine

what would be an elegant yet efficient way to accomplish it?

like image 496
Grzenio Avatar asked Jun 06 '12 14:06

Grzenio


1 Answers

There is no simple function to skip the first line in an array, because the operation is not efficient (it would have to copy the whole array), but you can do that easily if you use lazy sequences instead:

File.ReadAllLines(path) 
|> Seq.skip 1
|> Seq.map processLine

If you need the result in an array (as opposed to seq<'T>, which is an F# alias for IEnumerable<'T>), then you can add Seq.toArray to the end. However, if you just want to iterate over the lines later on, then you can probably just use sequences.

like image 111
Tomas Petricek Avatar answered Oct 14 '22 05:10

Tomas Petricek