Skip to content
This repository was archived by the owner on Feb 20, 2022. It is now read-only.

Commit d1c918a

Browse files
author
José Valim
committed
Briefly talk about anonymous functions before using them
1 parent 314eb18 commit d1c918a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ iex> :atom # An identifier (known as Symbols in other languages)
5252
iex> [1, 2, "three"] # Lists (typically hold a dynamic amount of items)
5353
[1, 2, "three"]
5454
iex> {:ok, "value"} # Tuples (typically hold a fixed amount of items)
55+
{:ok, "value"}
5556
```
5657

5758
Once we finish our portal application, we expect to be able to type the following code inside `iex`:
@@ -163,7 +164,16 @@ iex> [0|list]
163164

164165
Elixir data structures are immutable. In the examples above, we never mutated the list. We can break a list apart or add new elements to the head, but the original list is never modified.
165166

166-
That said, when we need to keep some sort of state, like the data transfering through a portal, we must use an abstraction that stores this state for us. One such abstraction in Elixir is called an agent:
167+
That said, when we need to keep some sort of state, like the data transfering through a portal, we must use an abstraction that stores this state for us. One such abstraction in Elixir is called an agent. To use agents, we need to briefly talk about anonymous functions before:
168+
169+
```iex
170+
iex> adder = fn a, b -> a + b end
171+
#Function<12.90072148/2 in :erl_eval.expr/5>
172+
iex> adder.(1, 2)
173+
3
174+
```
175+
176+
A anonymous function is delimited by the words `fn` and `end` and an arrow `->` is used to separate the arguments from the anonymous function body. We use anonymous functions to initialize, get and update the agent state:
167177

168178
```iex
169179
iex> {:ok, agent} = Agent.start_link(fn -> [] end)
@@ -180,7 +190,7 @@ iex> Agent.get(agent, fn list -> list end)
180190
181191
In the example above, we created a new agent, passing a function that returns the initial state of an empty list. The agent returns `{:ok, #PID<0.61.0>}`.
182192

183-
Curly brackets in Elixir specify a tuple; the tuple above contains the atom `:ok` and a process identifier (PID). We use atoms in Elixir as tags. In the example above, we are tagging the agents as successfully started.
193+
Curly brackets in Elixir specify a tuple; the tuple above contains the atom `:ok` and a process identifier (PID). We use atoms in Elixir as tags. In the example above, we are tagging the agent as successfully started.
184194

185195
The `#PID<...>` is a process identifier for the agent. When we say processes in Elixir, we don't mean Operating System processes, but rather Elixir Processes, which are light-weight and isolated, allowing us to run hundreds of thousands of them on the same machine.
186196

0 commit comments

Comments
 (0)