42
0b1010 # binary
0o17 # octal
0x1F # hex 3.14
1.0e-10 :hello
:ok
:error true # same as :true
false # same as :false "Hello, World!"
"Hello, #{name}!" # interpolation [1, 2, 3]
[1 | [2, 3]] # head | tail {:ok, "Success"}
{:error, reason} %{name: "Alice", age: 30}
%{"key" => "value"} [name: "Alice", age: 30] name = "Elixir" {a, b} = {1, 2} # a=1, b=2
[head | tail] = [1, 2, 3] x = 1
^x = 1 # match against value ==, !=, ===, !==
>, <, >=, <= and, or, not # strict boolean
&&, ||, ! # truthy/falsy "Hello" <> " World" [1, 2] ++ [3, 4] [1, 2, 3] -- [1] # [2, 3] if condition do
"true"
else
"false"
end unless condition do
"false branch"
end cond do
x > 0 -> "positive"
x < 0 -> "negative"
true -> "zero"
end case value do
{:ok, result} -> result
{:error, reason} -> reason
_ -> "default"
end with {:ok, a} <- fetch_a(),
{:ok, b} <- fetch_b(a) do
{:ok, a + b}
else
{:error, reason} -> {:error, reason}
end for x <- [1, 2, 3], do: x * 2 for x <- 1..10, rem(x, 2) == 0, do: x for x <- [1, 2], y <- [3, 4], do: {x, y} for {k, v} <- map, into: %{}, do: {k, v * 2} def sum([]), do: 0
def sum([h | t]), do: h + sum(t) def sum(list), do: sum(list, 0)
defp sum([], acc), do: acc
defp sum([h | t], acc), do: sum(t, acc + h) def greet(name) do
"Hello, #{name}!"
end def add(a, b), do: a + b defp helper(x), do: x * 2 def greet(name \\ "World"), do: "Hello, #{name}!" def handle({:ok, result}), do: result
def handle({:error, reason}), do: raise reason def abs(x) when x >= 0, do: x
def abs(x), do: -x add = fn a, b -> a + b end
add.(1, 2) # note the dot add = &(&1 + &2)
add.(1, 2) &String.upcase/1
&Enum.map/2 Enum.map([1, 2, 3], fn x -> x * 2 end)
Enum.map([1, 2, 3], &(&1 * 2)) defmodule MyModule do
def hello do
"Hello!"
end
end defmodule MyModule do
@my_constant 42
@moduledoc "Documentation"
end alias MyApp.Accounts.User
alias MyApp.Accounts.User, as: U import Enum, only: [map: 2, filter: 2] use GenServer require Logger
Logger.info("message") defmodule User do
defstruct name: "", age: 0
end user = %User{name: "Alice", age: 30} updated = %{user | age: 31} user.name %User{name: name} = user Enum.map([1, 2, 3], &(&1 * 2)) Enum.filter([1, 2, 3, 4], &(rem(&1, 2) == 0)) Enum.reduce([1, 2, 3], 0, &(&1 + &2)) Enum.find([1, 2, 3], &(&1 > 1)) Enum.any?([1, 2, 3], &(&1 > 2))
Enum.all?([1, 2, 3], &(&1 > 0)) Enum.count([1, 2, 3])
Enum.count([1, 2, 3], &(&1 > 1)) Enum.sort([3, 1, 2])
Enum.sort([3, 1, 2], :desc) Enum.group_by([1, 2, 3, 4], &rem(&1, 2)) Enum.zip([1, 2], [:a, :b]) [1, 2, 3]
|> Enum.map(&(&1 * 2))
|> Enum.filter(&(&1 > 2))
|> Enum.sum() 1..1_000_000
|> Stream.map(&(&1 * 2))
|> Stream.filter(&(&1 > 100))
|> Enum.take(10) pid = spawn(fn -> IO.puts("Hello") end) send(pid, {:hello, "world"}) receive do
{:hello, msg} -> IO.puts(msg)
_ -> IO.puts("Unknown")
after
1000 -> IO.puts("Timeout")
end self() spawn_link(fn -> raise "Error" end) defmodule Counter do
use GenServer
def start_link(initial) do
GenServer.start_link(__MODULE__, initial)
end
def init(initial), do: {:ok, initial}
def handle_call(:get, _from, state) do
{:reply, state, state}
end
def handle_cast(:inc, state) do
{:noreply, state + 1}
end
end GenServer.call(pid, :get) GenServer.cast(pid, :inc) defmodule MyTest do
use ExUnit.Case
test "the truth" do
assert 1 + 1 == 2
end
end assert value
refute value
assert_raise RuntimeError, fn -> ... end setup do
{:ok, user: %User{}}
end
test "with context", %{user: user} do
# use user
end describe "function_name/1" do
test "case 1" do
# ...
end
end mix test
mix test test/my_test.exs:10