Elixir basics · Lesson 09 (open contents)
Put code in a project
Write a module, meet def, files, and Mix, then head toward BEAM.
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".
Read from the first line down
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.
Symbols are not secret signs
defmoduleBegin defining a module.
def greet(name)Define the public function greet/1, which takes one argument.
do ... endWrap the code that belongs to a module or function.
Match each name to its meaning
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, and mix run.
Why these forms are useful
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.
Why this shape?
Why organize functions in modules before reaching for a class?
A module names related functions and creates a public boundary. Data remains an explicit argument instead of hiding behind this.
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.
Close the answer and try
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 mixTarget result: The project folder should contain mix.exs, lib/, and test/. After the default test passes, you should enter the project's IEx.
Stuck? Read one hint
Use village_path in both blanks. Run these commands in your system terminal, not in ordinary IEx.
After you run it, see one 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 mixThink about it: Is Mix the BEAM or another programming language?
Neither. Mix is Elixir's project tool. Elixir code still runs on the BEAM.
Remember these three lines
- 1
A module keeps related functions together.
- 2
An Elixir function returns the value of its last expression.
- 3
Mix creates, compiles, and tests projects.