<commit_before>local FS = require('fs');

local co
co = coroutine.create(function (filename)

  print("opening...")
  local err, fd = FS.open(co, filename, 'r', "0644")
  p("on_open", {err=err, fd=fd})
  if (err) then return end

  print("fstatting...")
  local err, stat = FS.fstat(co, fd)
  p("stat", {err=err, stat=stat})
  if (err) then return end

  print("reading...")
  local offset = 0
  repeat
    local err, chunk = FS.read(co, fd, offset, 128)
    local length = #chunk
    p("on_read", {err=err, chunk=chunk, offset=offset, length=length})
    if (err) then return end
    offset = offset + length
  until length == 0

  print("closing...")
  local err = FS.close(co, fd)
  p("on_close", {err=err})
  if (err) then return end

end)
coroutine.resume(co, "license.txt")

<commit_msg>Add a saner coroutine wrapper in the example<commit_after>local FS = require('fs');

-- Simple wrapper for coroutine steps
function fiber(fn)
  local co = coroutine.create(fn)
  assert(coroutine.resume(co, co))
end


fiber(function (co)

  print("opening...")
  local err, fd = FS.open(co, "license.txt", "r", "0644")
  p("on_open", {err=err, fd=fd})
  if (err) then return end

  print("fstatting...")
  local err, stat = FS.fstat(co, fd)
  p("stat", {err=err, stat=stat})
  if (err) then return end

  print("reading...")
  local offset = 0
  repeat
    local err, chunk = FS.read(co, fd, offset, 128)
    local length = #chunk
    p("on_read", {err=err, chunk=chunk, offset=offset, length=length})
    if (err) then return end
    offset = offset + length
  until length == 0

  print("closing...")
  local err = FS.close(co, fd)
  p("on_close", {err=err})
  if (err) then return end

end)


{'fraction_non_alphanumeric': 0.16834617664493184, 'fraction_numerical': 0.01066982809721399, 'mean_word_length': 3.549865229110512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4708267', 'n_tokens_mistral': 644, 'n_tokens_neox': 611, 'n_words': 183}