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

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

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions concepts/processes/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ In Elixir, all code runs inside processes.
By default, a function will execute in the same process from which it was called. When you need to explicitly run a certain function in a new process, use `spawn/1`:

```elixir
spawn(fn -> 2 + 2 end)
spawn(&my_function/0)
# => #PID<0.125.0>
```

`spawn/1` creates a new process that executes the given function and returns a _process identifier_ (PID). The new process will stay alive as long as the function executes, and then silently exit.
`spawn/1` creates a new process that executes the given 0-arity function and returns a _process identifier_ (PID). The new process will stay alive as long as the function executes, and then silently exit.

Elixir's processes should not be confused with operating system processes. Elixir's processes use much less memory and CPU. It's perfectly fine to have Elixir applications that run hundreds of Elixir processes.

Expand Down
4 changes: 3 additions & 1 deletion exercises/concept/take-a-number/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
## 1. Start the machine

- The machine should run in a new process. There is [a built-in function that starts a new process][kernel-spawn-1].
- You will need another function that the new process will execute.
- You will need another function that the new process will execute. You can name it, for example, `loop`.
- Use the [capture operator][special-forms-capture] to pass a named function as an argument.

## 2. Report the machine state

Expand Down Expand Up @@ -38,3 +39,4 @@
[kernel-spawn-1]: https://hexdocs.pm/elixir/Kernel.html#spawn/1
[kernel-receive]: https://hexdocs.pm/elixir/Kernel.SpecialForms.html#receive/1
[kernel-send]: https://hexdocs.pm/elixir/Kernel.html#send/2
[special-forms-capture]: https://hexdocs.pm/elixir/Kernel.SpecialForms.html#&/1
4 changes: 2 additions & 2 deletions exercises/concept/take-a-number/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ In Elixir, all code runs inside processes.
By default, a function will execute in the same process from which it was called. When you need to explicitly run a certain function in a new process, use `spawn/1`:

```elixir
spawn(fn -> 2 + 2 end)
spawn(&my_function/0)
# => #PID<0.125.0>
```

`spawn/1` creates a new process that executes the given function and returns a _process identifier_ (PID). The new process will stay alive as long as the function executes, and then silently exit.
`spawn/1` creates a new process that executes the given 0-arity function and returns a _process identifier_ (PID). The new process will stay alive as long as the function executes, and then silently exit.

Elixir's processes should not be confused with operating system processes. Elixir's processes use much less memory and CPU. It's perfectly fine to have Elixir applications that run hundreds of Elixir processes.

Expand Down
Loading