A module names related functions and creates a public boundary. Data remains an explicit argument instead of hiding behind this.
Put code in a project: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
Write a module, meet def, files, and Mix, then head toward BEAM.
Create a Mix project, run its default test, and open the project with iex -S mix.
Try it in IEx, then save it as lib/village.ex
Definitions in IEx disappear when the session ends. In a Mix project, save this module in lib/village.ex.
# A module keeps related functions together
defmodule Village do
# greet/1 takes a name and returns a greeting
def greet(name) do
"Hello, #{name}"
end
end
# Call it with Module.function(argument)
Village.greet("Maya")After the module is defined, IEx shows {:module, Village, ...}.
The final result is "Hello, Maya".
Steps for the full example
defmodule Village dobegins a module namedVillage.def greet(name)defines a public function. Its name-and-arity form isVillage.greet/1.- The value of a function's last expression becomes its return value automatically. There is no
returnkeyword. mix new villagecreates a project. Inside that folder,mix testruns tests andiex -S mixopens IEx with the project loaded.
Original lesson notes
IEx is like scratch paper. When code grows, put related functions in a module and save it in a file.
defmodule defines a module. def defines a function that code outside the module can call. Module names usually use CamelCase and begin with a capital letter.
Mix manages Elixir projects. It creates folders, compiles code, and runs tests. Mix comes with Elixir. It is not a third programming language.
Symbols and terms
defmodule- Begin defining a module.
def greet(name)- Define the public function
greet/1, which takes one argument. do ... end- Wrap the code that belongs to a module or function.
- module
- A shared name for a group of related functions, such as
Village. - source file
- A text file that stores code. Elixir source files often end in
.ex. - Mix
- Elixir's project tool. Common commands include
mix new,mix test, andmix run.
Original full exercise
Create a Mix project in your system terminal. Run its default tests, then open IEx with the project loaded.
# Use the same project folder name in both blanks
mix new ____
cd ____
# Run the tests included with the new project
mix test
# Open IEx with this project loaded
iex -S mixThe project folder should contain mix.exs, lib/, and test/. After the default test passes, you should enter the project's IEx.
Show hint
Use village_path in both blanks. Run these commands in your system terminal, not in ordinary IEx.
Show original answer
# Create the project and enter its folder
mix new village_path
cd village_path
# Run the default test once
mix test
# Try project modules here later
iex -S mixIs Mix the BEAM or another programming language?
Neither. Mix is Elixir's project tool. Elixir code still runs on the BEAM.
Original lesson takeaways
- A module keeps related functions together.
- An Elixir function returns the value of its last expression.
- Mix creates, compiles, and tests projects.
Design trade-offs and language comparisons
Why this shape?
Why organize functions in modules before reaching for a class?
Add a process only when state needs an owner, a lifecycle, or failure isolation. A GenServer around a pure function merely adds a queue.
Coming from Java, Python, or JavaScript
Java
- Familiar starting point
- A class combines data, methods, and object identity.
- What BEAM changes
- A BEAM module is not an object, and functions have no implicit receiver.
- False friend
- Calling a module does not start a process.
Python
- Familiar starting point
- Modules and classes can both organize code.
- What BEAM changes
- Module/function/arity forms the stable entry point; state is a separate design decision.
- False friend
- An OTP Application is not an operating-system process.
JavaScript
- Familiar starting point
- ES modules export functions or classes.
- What BEAM changes
- BEAM calls compiled modules by module name and arity.
- False friend
- Mix and rebar3 manage projects; neither is BEAM or OTP.