Skip to content

Commit

Permalink
Take-a-number needs a named function example (#1486)
Browse files Browse the repository at this point in the history
* Take-a-number needs a named function example

* Edit the source file
  • Loading branch information
angelikatyborska authored May 26, 2024
1 parent 8aba4d8 commit dc47840
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
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

0 comments on commit dc47840

Please sign in to comment.