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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With