Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# Agent An `Agent` is a specialized `GenServer` that provides a set of helper functions which make it easy to store and update state in-memory using a process. Agents come in handy if you want a simple GenServer to hold onto state. ```elixir defmodule RunElixir.GameState do use Agent # Public @initial_state %{ rounds_played: 0, current_points: 0, total_points: 0 } # Starts a new Agent process with an initial state. def start_link(_args) do Agent.start_link(fn -> @initial_state end) end # Creates a fire-and-forget message to the Agent. # # Unlike in a GenServer, we don't have to write callbacks # for our events like `handle_cast/2` or `handle_call/3` # but can define the callback as an anonymous function. # # The anonymous function receives the current state # and has to return the new state. def add_points(pid, points) do Agent.cast(pid, fn state -> Map.update!(state, :current_points, &(&1 + points)) end) end # Returns the current state. def get_state(pid), do: Agent.get(pid, fn state -> state end) # Ends the round by updating and returning the state. # # `Agent.get_and_update/2` receives the current state in the # anonymous function and has to return a tuple with the state # that should be returned to the calling process and # the new process state. def end_round(pid) do Agent.get_and_update(pid, fn state -> %{ rounds_played: rounds_played, total_points: total_points, current_points: current_points } = state new_state = %{ rounds_played: rounds_played + 1, total_points: total_points + current_points, current_points: 0 } {new_state, new_state} end) end end {:ok, pid} = RunElixir.GameState.start_link([]) RunElixir.GameState.get_state(pid) |> IO.inspect(label: 1) RunElixir.GameState.add_points(pid, 100) |> IO.inspect(label: 2) RunElixir.GameState.get_state(pid) |> IO.inspect(label: 3) RunElixir.GameState.end_round(pid) |> IO.inspect(label: 4) ``` <!-- livebook:{"output":true} --> ``` 1: %{current_points: 0, total_points: 0, rounds_played: 0} 2: :ok 3: %{current_points: 100, total_points: 0, rounds_played: 0} 4: %{current_points: 0, total_points: 100, rounds_played: 1} ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run on your machine

with Livebook Desktop

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More ×