exercism / elixir

Exercism exercises in Elixir.
https://exercism.org/tracks/elixir
MIT License
614 stars 397 forks source link

Take-a-number needs a named function example #1486

Closed angelikatyborska closed 4 months ago

angelikatyborska commented 4 months ago

Partially addresses https://github.com/exercism/elixir/issues/1482

In take-a-number, it was difficult to figure out how to pass a named function to spawn. The example in the introduction used an anonymous function, and we actually don't teach anywhere how to use the capture operator to pass named functions as arguments. The only place this is mentioned, is the anonymous function's concept's "about" page, which most people will never see (rendered only if you browse the concept after learning it). None of the concept introductions mention how to use the capture operator for named functions.

github-actions[bot] commented 4 months ago

Thank you for contributing to exercism/elixir 💜 🎉. This is an automated PR comment 🤖 for the maintainers of this repository that helps with the PR review process. You can safely ignore it and wait for a maintainer to review your changes.

Based on the files changed in this PR, it would be good to pay attention to the following details when reviewing the PR:


Automated comment created by PR Commenter 🤖.

angelikatyborska commented 1 month ago

A bit late, but I noticed now that the exemplar solution doesn't even use function capturing 🙃 I'm afraid this change might have added even more confusion...

defmodule TakeANumber do
  def start() do
    spawn(fn -> loop(0) end)
  end

  defp loop(state) do
    receive do
      {:report_state, sender_pid} ->
        send(sender_pid, state)
        loop(state)

      {:take_a_number, sender_pid} ->
        state = state + 1
        send(sender_pid, state)
        loop(state)

      :stop ->
        nil

      _ ->
        loop(state)
    end
  end
end