Skip to content

Instantly share code, notes, and snippets.

@zindel
Created November 7, 2018 09:54
Show Gist options
  • Select an option

  • Save zindel/54ac6b9e920643aafe7f96c035d1aa9b to your computer and use it in GitHub Desktop.

Select an option

Save zindel/54ac6b9e920643aafe7f96c035d1aa9b to your computer and use it in GitHub Desktop.
module Action : sig
type _ t
val execute : 'a t -> 'a
val readFile : string -> string t
val writeFile : string -> string -> unit t
end = struct
type _ t =
| ReadFile : string -> string t
| WriteFile : string * string -> unit t
let readFile f = ReadFile f
let writeFile f data = WriteFile (f, data)
let execute : type res. res t -> res = function
| ReadFile f ->
let ic = open_in f in
let rec read acc =
try
let acc = input_line ic :: acc in
read acc
with End_of_file -> acc
in
let data = read [] in
close_in ic ; String.concat "\n" data
| WriteFile (f, data) ->
let oc = open_out f in
Printf.fprintf oc "%s\n" data ;
close_out oc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment