Most people understand the concept of vending machines. I think building one in Elixir can help illustrate how it can be done in a functional language. The project is not completed but I think I have enough to illustrate some concepts.
This project will be broken into two parts.
- Example of functional concepts like immutable state.
- Example of
set_upanddescribewriting unit tests. - Example of how to write errors and the conventions.
We will build this with one module and walk through how to:
- Load up a machine
- Insert funds.
- Refund funds.
- Interacting with sold out or not enough inventory available.
We will then incorporate the module from Part 1 into a GenServer. This will be able to hold state and do some other interesting things for us.
The test directory illustrates all the use cases and does a good overview of how setup and testing works. I think one of the nice things about Elixir is how easy it is to test your projects.
Final Clean Architecture:
VendingMachine # Public API - clean interface
├── VendingMachine.Core # Business logic - pure functions
├── VendingMachine.Server # Process management - GenServer
├── VendingMachine.Inventory # Data structures
└── VendingMachine.Validations # Validation rules
cd vending_machine
mix test
..........
Finished in 0.06 seconds (0.00s async, 0.06s sync)
10 tests, 0 failures
