Demonstration / training over some Elixir concepts for iWT Health.
- Initial Umbrella Application
mix new repeater --umbrella
- New app
mix new repeater
- Show basic testing
- assert
- file_name_test.exs
- Basic Documentation
- parameters
- doc tests
- Demonstrate basic doc test failure
- Immutability (basic)
- Show repeater tests
- Parameter guard
- Messed up the doc test (“5” instead of 5)
- Show iex -S mix
- Show recompile (change Repeating to Repeat - )
- Parameter matching
- Case pattern matching
- Cond control structure
- After show the with statement from this article:
- Elixir's With Statement
- Function matching - structs
- Show namespace structure
- Basic process spawning
Repeater.Loop.start(“test”)
- synchronous loop, “blocking”Repeater.Loop.async(“foo”)
- asynchronous loop
- Agent
- Holds state only
- Module-less: https://elixirschool.com/en/lessons/advanced/concurrency/#agents◦
- Show only running specific tests
- New Supervisor App
mix new chat --sup
- Creates same app but with /lib/chat/application.ex
- GenServer
- Holds state and processes
- Note the cast / call differences
- Chat.Supervisor
- Note that there isn’t a way to get the only server, somewhat useless
- Add :chat_room atom name to Chat.Server
- Demonstrate in
iex -S mix
Chat.Server.get_messages(:chat_room)
Chat.Server.add_message(:chat_room, “test”)
- Demonstrate supervisor restarting
Process.whereis(:chat_room)
Process.whereis(:chat_room) |> Process.exit(:kill)
Process.whereis(:chat_room)
- Name registration from gproc & via_tuple
- via_tuple - Tells Elixir how to use a custom registry to register our process.
{:via, module, term}
{:via, gproc, gproc parameters}
- gproc params
- :n - name
- :l - local, meaning not registered on this cluster
- {:chat_room, room_name} - actual name