Skip to content

Commit

Permalink
Take-a-number needs a named function example
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed May 25, 2024
1 parent 8aba4d8 commit 8ceca8f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
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

0 comments on commit 8ceca8f

Please sign in to comment.