You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 20, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+12-2Lines changed: 12 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,6 +52,7 @@ iex> :atom # An identifier (known as Symbols in other languages)
52
52
iex> [1, 2, "three"] # Lists (typically hold a dynamic amount of items)
53
53
[1, 2, "three"]
54
54
iex> {:ok, "value"} # Tuples (typically hold a fixed amount of items)
55
+
{:ok, "value"}
55
56
```
56
57
57
58
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]
163
164
164
165
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.
165
166
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:
@@ -180,7 +190,7 @@ iex> Agent.get(agent, fn list -> list end)
180
190
181
191
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>}`.
182
192
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.
184
194
185
195
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.
0 commit comments