IO.inspect [1, 2, true, "abc"] ++ [3, 2, "abc"] # Concatenate two lists. IO.inspect [1, 2, true, "abc"] -- [3, 2, "abc"] # List subtraction. IO.inspect(hd [1, 2, 3]) # Head of a list. IO.inspect(tl [1, 2, 3]) # Tail of a list. tuple = {:ok, "hello"} IO.inspect(elem tuple, 1) # Get the second element of a tuple. IO.inspect "ABC" <> "XYZ" # String concatenation. l = [1, 2, 3] [h | t] = l IO.inspect {h, t} IO.inspect [0 | l] a = 2 {^a, b} = {2, 42} # Pin operator. IO.inspect b, label: "b" {x, x} = {10, 10} # 'x' must be bind to the same value. IO.inspect x, label: "x" IO.puts "=== Binary ===" str = "hëllö" <> <<0>> # Append a zero to the string (binary). IO.inspect str, binaries: :as_binaries IO.inspect(<<3::4>> === <<0::1, 0::1, 1::1, 1::1>>) # Same binaries: 3 encoded on 4 bits. IO.inspect(is_binary <<0xFF, 0x5A>>) # true. IO.inspect(is_binary <<0xFF, 0xF::4>>) # false: binaries must be on a multiple of 8 bits. IO.inspect(is_bitstring <<0xFF, 0xF::4>>) # true. # Pattern matching on binaries. <> = "über" IO.inspect(x === ?ü) # true. # 'charlist': the string in Erlang. IO.puts "=== charlist ===" IO.inspect('hëllò', charlists: :as_lists) IO.inspect(to_string('hëllò') == "hëllò") # true. IO.inspect('hëllò' == to_charlist("hëllò")) # true. # keyword list. IO.puts "=== keyword list ===" IO.inspect([{:a, 1}, {:b, 2}] === [a: 1, b: 2]) # true. IO.inspect(a: 1, b: 2, c: 3) # Brackets omitted. # Maps. IO.puts "=== Map ===" map = %{:a => 1, 2 => :b} IO.inspect map[:a] # Print 1. IO.inspect map.a # Special syntax with atom key, print 1. IO.inspect map[2] # Print ':b'. IO.inspect map[3] # Print 'nil'. %{:a => a} = map IO.inspect a # Print 1. IO.inspect %{map | 2 => "two"} # Updating 'map'. # Module. IO.puts "=== Module ===" defmodule Math do def sum(a, b) do a + b end def zero?(0), do: true def zero?(x) when is_integer(x), do: false end IO.inspect Math.sum(1, 2) IO.inspect Math.zero?(1) fun = &Math.zero?/1 # '&' to capture a function as a lambda (and use with high level functions). IO.inspect fun.(0) # Calling it with same syntaxe as lambdas. fun2 = &(&1 + 1) # Shortcut syntax: '&1' is the first parameter. fun3 = &"Good #{&1}" IO.inspect fun2.(3) # Print 4. # Enum. IO.puts "=== Enum ===" IO.inspect Enum.map([1, 2, 3], &(2 * &1)) IO.inspect Enum.map(%{1 => 2, 3 => 4}, fn {k, v} -> k * v end) IO.inspect Enum.map(1..10, &(3 * &1)) IO.inspect Enum.reduce(1..3, 0, &+/2) # 1 + 2 + 3. odd? = &(rem(&1, 2) != 0) IO.inspect 1..100_000 |> Enum.map(&(3 * &1)) |> Enum.filter(odd?) |> Enum.sum # Process. IO.puts "=== Process ===" pid = spawn fn -> receive do {:hello, sender, msg} -> IO.puts "Message received: '#{msg}'" send sender, :ok end end send pid, {:hello, self(), "Salut"} receive do :ok -> IO.puts "Message delivered!" after 1_000 -> IO.puts "Message not delivered :(" end IO.puts "=== State ===" defmodule KV do # Key-Value. def stark_link do Task.start_link(fn -> loop %{} end) end defp loop(map) do receive do {:get, key, caller} -> send caller, Map.get(map, key) loop map {:put, key, value} -> loop Map.put(map, key, value) end end end {:ok, kv} = KV.stark_link send kv, {:put, :a, 42} send kv, {:put, :b, 420} send kv, {:get, :b, self()} receive do value -> IO.puts "Value received: #{value}" end IO.puts "=== Agent ===" {:ok, pid} = Agent.start_link(fn -> %{} end) Agent.update(pid, fn map -> Map.put(map, :hello, :world) end) IO.inspect Agent.get(pid, fn map -> Map.get(map, :hello) end)