Created
March 22, 2011 20:30
-
-
Save alexy/881981 to your computer and use it in GitHub Desktop.
return a list of lines in a file, with an optional skip number and max on actually returned ones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let read_lines ?skip ?maxn filename = | |
let ic = open_in filename in | |
let step,ctr = make_step_counter 0 in | |
E.from_while begin fun () -> | |
try | |
match skip,maxn with | |
| (Some s, _) when ctr () < s -> | |
ignore (input_line ic); step (); Some None | |
| (skip, Some n) when n > 0 && ctr () - (Option.default 0 skip) = n -> | |
close_in ic; None | |
| _ -> | |
let line = input_line ic in step (); Some (Some line) | |
with End_of_file -> None | |
end |> | |
E.filter_map identity |> L.of_enum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment