Skip to content

Instantly share code, notes, and snippets.

@alexy
Created March 22, 2011 20:30
Show Gist options
  • Save alexy/881981 to your computer and use it in GitHub Desktop.
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
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