15. defmodule Teste do
def say(name) do
IO.puts("Hello #{name}")
end
end
Teste.say("Fabio")
# => "Hello Fabio"
defmodule Teste do
@spec say(String.t)
def say(name) when is_string(name) do
IO.puts "Hello " <> name
end
end
TiposeParêntesesOpcionais
16. defmodule Teste do
def factorial(n) do
if n == 0 do
1
else
n * factorial(n - 1)
end
end
end
Teste.factorial(10)
# => 3628800
defmodule Teste do
def factorial(1), do: 1
def factorial(n) do
n * factorial(n - 1)
end
end
Teste.factorial(10)
# => 3628800
CallbyPattern
17. %{last_name: name} = registro
# => ** (MatchError) no match of right hand side value:
%{foo: {:ok, [1, 2, 3, 4]}, name: "Fabio", year: 2017}
registro = %{name: "Fabio", year: 2017, foo: {:ok, [1,2,3,4]}}
%{name: name, foo: {:ok, result}} = registro
IO.puts Enum.join([name|result], ", ")
# => Fabio, 1, 2, 3, 4
registro = {:ok, %{name: "Fabio", last_name: "Akita"}}
{:ok, %{name: name}} = registro
IO.puts name
# => Fabio
{:ok, result} = Enum.fetch([1,2,3,4,5], 3)
IO.puts result
# => 4
registro = {:ok, %{name: "Fabio", last_name: "Akita"}}
{:ok, %{name: name}} = registro
IO.puts name
# => Fabio
registro = %{name: "Fabio", year: 2017, foo: {:ok, [1,2,3,4]}}
%{name: name, foo: {:ok, result}} = registro
IO.puts Enum.join([name|result], ", ")
# => Fabio, 1, 2, 3, 4
%{last_name: name} = registro
# => ** (MatchError) no match of right hand side value:
%{foo: {:ok, [1, 2, 3, 4]}, name: "Fabio", year: 2017}
PatternMatching
22. iex(22)> function = fn -> IO.puts("Hello from function") end
#Function<20.99386804/0 in :erl_eval.expr/5>
iex(23)> function.()
Hello from function
:ok
iex(24)> pid = spawn(function)
Hello from function
#PID<0.112.0>
iex(25)> Process.alive?(pid)
false
iex(26)> Process.alive?(self)
true
iex(27)> self
#PID<0.85.0>
iex(22)> function = fn -> IO.puts("Hello from function") end
#Function<20.99386804/0 in :erl_eval.expr/5>
iex(23)> function.()
Hello from function
:ok
iex(24)> pid = spawn(function)
Hello from function
#PID<0.112.0>
iex(25)> Process.alive?(pid)
false
iex(26)> Process.alive?(self)
true
iex(27)> self
#PID<0.85.0>
24. Processos
• Hiper-leves (pode subir milhares)
• Totalmente isoladas (Process.send(pid, :kill))
• Comunicação por passagem de mensagens
• Um mailbox por processo (receive)
• Garbage Collector separado por Processo!
27. defmodule Stack do
def start_link do
Agent.start_link(fn -> [] end, name: __MODULE__)
end
def push(new_value) do
Agent.update(__MODULE__,
fn(state) ->
[new_value|state]
end)
end
def pop do
Agent.get_and_update(__MODULE__,
fn(state) ->
[head|tail] = state
{head, tail}
end)
end
end
28. iex(8)> Stack.start_link
{:ok, #PID<0.123.0>}
iex(9)> Stack.push "hello"
:ok
iex(10)> Stack.push "world"
:ok
iex(11)> Stack.pop
"world"
iex(12)> Stack.pop
"hello"
iex(13)> Process.list |> Enum.reverse |> Enum.at(0) |>
Process.exit(:kill)
** (EXIT from #PID<0.85.0>) evaluator process exited with reason:
killed
Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER
for help)
iex(1)> Stack.push "foo"
** (exit) exited in: GenServer.call(Stack, {:update,
#Function<1.117587580/1 in Stack.push/1>}, 5000)
** (EXIT) no process: the process is not alive or there's no
process currently associated with the given name, possibly because
its application isn't started
(elixir) lib/gen_server.ex:766: GenServer.call/3
iex(8)> Stack.start_link
{:ok, #PID<0.123.0>}
iex(9)> Stack.push "hello"
:ok
iex(10)> Stack.push "world"
:ok
iex(11)> Stack.pop
"world"
iex(12)> Stack.pop
"hello"
iex(13)> Process.list |> Enum.reverse |> Enum.at(0) |>
Process.exit(:kill)
** (EXIT from #PID<0.85.0>) evaluator process exited with reason:
killed
Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER
for help)
iex(1)> Stack.push "foo"
** (exit) exited in: GenServer.call(Stack, {:update,
#Function<1.117587580/1 in Stack.push/1>}, 5000)
** (EXIT) no process: the process is not alive or there's no
process currently associated with the given name, possibly because
its application isn't started
(elixir) lib/gen_server.ex:766: GenServer.call/3
29. defmodule Stack.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, :ok)
end
def init(:ok) do
children = [ worker(Stack, []) ]
supervise(children, strategy: :one_for_one)
end
end