This commit is contained in:
Rhiannon Morris 2020-12-02 10:31:32 +01:00
parent 29d8387656
commit 512ae7d3d7
5 changed files with 257 additions and 5 deletions

17
bracket.ml Normal file
View file

@ -0,0 +1,17 @@
let bracket ~make ~act ~free =
let x = make () in
match act x with
| result -> free x; result
| exception e -> free x; raise e
let infile ~act name =
bracket ~make:(fun () -> open_in name) ~act ~free:close_in_noerr
let infile_lines ~line:f ~of_seq name =
let get_line file =
match input_line file with
| exception End_of_file -> None
| line -> Some (f line, file)
in
let act file = of_seq (Seq.unfold get_line file) in
infile name ~act