-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
Take-a-number needs a named function example #1486
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small increment, but worth doing, thank you.
Don't forget to make the same change in the original into/about files.
Thank you for contributing to 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 🤖. |
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 |
This reverts commit dc47840.
Partially addresses #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.