Skip to content

Instantly share code, notes, and snippets.

@squadette
Created October 18, 2017 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squadette/cda3c1e40d2c3c7cdcf3d7e9c3da015b to your computer and use it in GitHub Desktop.
Save squadette/cda3c1e40d2c3c7cdcf3d7e9c3da015b to your computer and use it in GitHub Desktop.
# given file path, print number of lines or report I/O errors from opening/reading
with file_contents(filename) do
case error_free (contents):
print number_of_lines(contents)
case io_error(errno):
print "I/O error opening file: #{perror(errno)}"
end
end
def file_contents(filename) : { error_free: contents, io_error: errno }
with syscall_open(filename) do
case error_free (fd):
with syscall_read(fd) do
case error_free(contents):
yield error_free<contents>
case io_error(errno):
yield io_error<errno>
end
end
case io_error (errno):
yield io_error<errno>
end
end
def syscall_open(filename) : { error_free: fd, io_error: errno }
# ...FFI...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment