Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make examples more didactic #6

Open
pmetras opened this issue Jan 15, 2022 · 0 comments
Open

Make examples more didactic #6

pmetras opened this issue Jan 15, 2022 · 0 comments

Comments

@pmetras
Copy link

pmetras commented Jan 15, 2022

The examples are used as tests to validate the fork/join pattern, but they can be made more didactic for Pony learners.

For instance, all examples output the completion of the join by writing the result in the Collector finish function, but frequently one will need to get that result into the actor that initiated the fork/join process. So instead of printing the output in the Collector, having the example outputting the result in Main actor would show how the result can be sent back to the initiator.

Also, inserting Debug with digestof this into the functions of the three classes *Worker, *Generator and *Collector of the examples would show the users who run those examples in debug mode how the work is split between multiple actors and get collected at the end.

Sample for process-array.pony

class Adder is fj.Worker[Array[U8] iso, USize]
  var _working_set: Array[U8] = _working_set.create()

  fun ref receive(work_set: Array[U8] iso) =>
    Debug("Adder #" + (digestof this).string() + " receiving work to do")
    _working_set = consume work_set

  fun ref process(runner: fj.WorkerRunner[Array[U8] iso, USize] ref) =>
    Debug("Adder #" + (digestof this).string() + " doing work")
    var total: USize = 0

    for i in _working_set.values() do
      total = total + i.usize()
    end

    Debug("Adder #" + (digestof this).string() + " completed work")
    runner.deliver(total)

Last, adding how to use the word-count.pony example in case of launch error is more user friendly than reporting an error:

      env.exitcode(-1)
      env.err.print("Error during setup.")
      env.err.print("Usage: word-count <filename>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant