Managing State with Processes
Functional programs are stateless, but we still need to be able to manage state. In Elixir, we use concurrent processes and recursion to handle this task. That may sound counterintuitive, but let’s take a look at how it works with a simple program.
To start with, let’s create a child application. From the rumbl_umbrella root directory, change to apps and create a new mix project, like this:
| $ ➔ cd apps |
| $ ➔ mix new info_sys --sup |
| * creating README.md |
| ... |
| $ ➔ cd info_sys |
We create a brand new mix project under the apps directory. Later it will evolve into our full service, but for now let’s create a Counter server that counts up or down. Create a apps/info_sys/lib/info_sys/counter.ex ...
Get Programming Phoenix 1.4 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.