Elixir basics · Lesson 09 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · SCRATCH · LESSON 0935 minutes

Put code in a project

Write a module, meet def, files, and Mix, then head toward BEAM.

01 · Start with the whole example

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.

Elixir
# 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")
Check the result first
  • After the module is defined, IEx shows {:module, Village, ...}.
  • The final result is "Hello, Maya".
02 · Take the code apart

Read from the first line down

  1. defmodule Village do begins a module named Village.
  2. def greet(name) defines a public function. Its name-and-arity form is Village.greet/1.
  3. The value of a function's last expression becomes its return value automatically. There is no return keyword.
  4. mix new village creates a project. Inside that folder, mix test runs tests and iex -S mix opens IEx with the project loaded.
03 · Meet the new symbols

Symbols are not secret signs

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.

04 · Ideas inside the code

Match each name to its meaning

01

module

A shared name for a group of related functions, such as Village.

02

source file

A text file that stores code. Elixir source files often end in .ex.

03

Mix

Elixir's project tool. Common commands include mix new, mix test, and mix run.

05 · Make the idea clear

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.

DESIGN DECISION

Why this shape?

Why organize functions in modules before reaching for a class?

Choice

A module names related functions and creates a public boundary. Data remains an explicit argument instead of hiding behind this.

Cost and boundary

Add a process only when state needs an owner, a lifecycle, or failure isolation. A GenServer around a pure function merely adds a queue.

A FAMILIAR POINT OF VIEW

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.
06 · Change it yourself

Close the answer and try

Create a Mix project in your system terminal. Run its default tests, then open IEx with the project loaded.

Practice starting point
# 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 mix

Target 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
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 mix
Think about it: Is Mix the BEAM or another programming language?

Neither. Mix is Elixir's project tool. Elixir code still runs on the BEAM.

Take with you

Remember these three lines

  1. 1

    A module keeps related functions together.

  2. 2

    An Elixir function returns the value of its last expression.

  3. 3

    Mix creates, compiles, and tests projects.

Lesson completeRun the code once, then mark this lesson as done.