I just started playing with F #, so this question is likely to be pretty simple ...
I would like to read a text file line by line, and then ignore the first line and process the other lines using this function. So I was thinking about using something like:
File.ReadAllLines(path) |> Array.(ignore first element) |> Array.map processLine
What would be an elegant but effective way to achieve it?
There is no simple function to skip the first line in the array, because the operation is inefficient (it would have to copy the entire array), but you can do it easily if you use lazy sequences instead:
File.ReadAllLines(path) |> Seq.skip 1 |> Seq.map processLine
( seq<'T>, F # IEnumerable<'T>), Seq.toArray . , , , , .
seq<'T>
IEnumerable<'T>
Seq.toArray
, , , . , , , , . ( , ). Seq.skip . , , - :
System.IO.File.ReadAllLines fileName |> Seq.mapi (fun i elem -> i, elem) |> Seq.choose (fun (i, elem) -> if i > 0 then Some(elem) else None)
You will skip the first element in the F # array simply myArray.[1..]. Gotta love how elegant this language is.
myArray.[1..]