Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
ELIXIRTolerância a Falhas para Adultos
Fabio Akita
@akitaonrails@akitaonrails
Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
Chris McCordChris McCord José ValimJosé Valim
Joe Armstrong - EricssonJoe Armstrong - Ericsson
defmodule Teste do
def say(name) do
IO.puts("Hello #{name}")
end
end
Teste.say("Fabio")
defmodule Teste do
@spec say(Strin...
OPTIONAL PARENTHESIS
-module(teste).
-export([qsort/1]).
qsort([]) -> [];
qsort([Pivot|T]) ->
qsort([X||X<-T,X =< Pivot]) ++
[Pivot] ++
qsort([...
defmodule Teste do
def factorial(n) do
if n == 0 do
1
else
n * factorial(n - 1)
end
end
end
Teste.factorial(10)
# => 36288...
CALL BY PATTERN
{:ok, result} = Enum.fetch([1, 2, 3, 4], 3)
IO.puts result
# => 4
registro = {:ok, %{name: "Fabio", last_name: "Akita"}}
{...
PATTERN MATCHING
(“Regex for non-String”)
a = 1
a = 2
IO.puts a
# => 2
^a = 3
# => ** (MatchError) no match of right hand side value: 3
message = "Hello"
IO.puts me...
range = (1..100)
interval = Enum.slice(range, 30, 10)
evens = Enum.filter(interval, fn(n) -> rem(n, 2) == 0 end)
multiplie...
range = (1..100)
interval = Enum.slice(range, 30, 10)
evens = Enum.filter(interval, fn(n) -> rem(n, 2) == 0 end)
multiplie...
range = (1..100)
interval = Enum.slice(range, 30, 10)
evens = Enum.filter(interval, fn(n) -> rem(n, 2) == 0 end)
multiplie...
(1..100)
|> Enum.slice(30, 10)
|> Enum.filter(&(rem(&1, 2)))
|> Enum.map(&(&1 * 10))
|> Enum.take(2)
(1..100)
|> Enum.slic...
PIPE OPERATOR
|>
function = fn -> IO.puts("Hello from function") end
function.()
# => Hello from function
pid = spawn(function)
# => Hello ...
pid = spawn(fn ->
receive do
{:say, from} ->
send(from, "say what?")
_ ->
Process.exit(self, :normal)
end
end)
Process.ali...
“PROCESSES”
Lightweight
Isolated
Message passing
Mailbox
Garbage Collected
pid = spawn(fn -> ... end)
Process.exit(pid, :kill)
pid = spawn_link(fn ->
receive do
{:say, from} ->
send(from, "say what?")
_ ->
Process.exit(self, :normal)
end
end)
send(p...
pid = spawn_link(fn -> ... end)
Process.exit(pid, :kill)
pid = spawn_link(fn ->
receive do
{:say, from} ->
send(from, "say what?")
_ ->
Process.exit(self, :normal)
end
end)
Proces...
ASYNCHRONOUS
EXCEPTIONS
(spawn_link, flag, exit)
“GLOBALS”
Javascript
Scala
Go
Clojure
...
defmodule Stack do
def start_link do
Agent.start_link(fn -> [] end,
name: __MODULE__)
end
def pop do
Agent.get_and_update(...
Stack.start_link
Stack.push "hello"
Stack.push "world"
Stack.pop
# => world
Stack.pop
# => hello
Process.list |> Enum.reve...
defmodule Stack.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, :ok)
end
def init(:ok) do...
Stack.Supervisor.start_link
Stack.push "hello"
Stack.push "world"
Stack.pop
# => world
Stack.pop
# => hello
Process.list |...
Stack.Supervisor.start_link
Process.list |> Enum.reverse |> Enum.at(0) |> Process.exit(:kill)
“YOCTO” SERVICES
(micro > nano > pico > femto > atto > zepto > yocto>
APRENDENDO
OBRIGADO!
Perguntas?
@akitaonrails
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba
Upcoming SlideShare
Loading in …5
×

Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba

514 views

Published on

Reformulação da minha palestra de introdução a o que torna Erlang - e por consequência, Elixir - perfeito e único para sistemas altamente distribuídos e de alta disponibilidade.

Published in: Technology
  • Login to see the comments

Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba

  1. 1. ELIXIRTolerância a Falhas para Adultos Fabio Akita
  2. 2. @akitaonrails@akitaonrails
  3. 3. Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
  4. 4. Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
  5. 5. Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
  6. 6. Chris McCordChris McCord José ValimJosé Valim
  7. 7. Joe Armstrong - EricssonJoe Armstrong - Ericsson
  8. 8. defmodule Teste do def say(name) do IO.puts("Hello #{name}") end end Teste.say("Fabio") defmodule Teste do @spec say(String.t) def say(name) when is_string(name) do IO.puts "Hello " <> name end end Teste.say "Fabio"
  9. 9. OPTIONAL PARENTHESIS
  10. 10. -module(teste). -export([qsort/1]). qsort([]) -> []; qsort([Pivot|T]) -> qsort([X||X<-T,X =< Pivot]) ++ [Pivot] ++ qsort([X||X<-T,X > Pivot]). defmodule Teste do def qsort([]), do: [] def qsort([pivot|t]) do qsort(for x <- t, x < pivot, do: x) ++ [pivot] ++ qsort(for x <- t, x >= pivot, do: x) end end
  11. 11. 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(0), do: 1 def factorial(n) do n * factorial(n - 1) end end Teste.factorial(10) # => 3628800
  12. 12. CALL BY PATTERN
  13. 13. {:ok, result} = Enum.fetch([1, 2, 3, 4], 3) IO.puts result # => 4 registro = {:ok, %{name: "Fabio", last_name: "Akita"}} {:ok, %{name: name}} = registro IO.puts name # => Fabio registro = %{name: "Fabio", year: 2016, foo: {:ok, [1, 2, 3]}} %{name: name, foo: {:ok, result}} = registro IO.puts Enum.join([name|result], ", ") # => Fabio, 1, 2, 3 %{last_name: name} = registro # => ** (MatchError) no match of right hand side value: ...
  14. 14. PATTERN MATCHING (“Regex for non-String”)
  15. 15. a = 1 a = 2 IO.puts a # => 2 ^a = 3 # => ** (MatchError) no match of right hand side value: 3 message = "Hello" IO.puts message <> " World!" # => "Hello World! IO.puts "#{message} World!" # => "Hello World! {:a, "b", 67, true} [:a, "b", 67, true] %{a: 1, b: 2, c: 3} [a: 1, b: 2, c: 3] # => [{:a, 1}, {:b, 2}, {:c, 3}]
  16. 16. range = (1..100) interval = Enum.slice(range, 30, 10) evens = Enum.filter(interval, fn(n) -> rem(n, 2) == 0 end) multiplied = Enum.map(evens, fn(n) -> n * 10 end) Enum.take(multiplied, 2) range = (1..100) # => [1, 2, 3, 4 ... 98, 99, 100] interval = Enum.slice(range, 30, 10) # => [31, 32, 33, 34, 35, 36, 37, 38, 39, 40] evens = Enum.filter(interval, fn(n) -> rem(n, 2) == 0 end) # => [32, 34, 36, 38, 40] multiplied = Enum.map(evens, fn(n) -> n * 10 end) # => [320, 340, 360, 380, 400] Enum.take(multiplied, 2) # => [320, 340]
  17. 17. range = (1..100) interval = Enum.slice(range, 30, 10) evens = Enum.filter(interval, fn(n) -> rem(n, 2) == 0 end) multiplied = Enum.map(evens, fn(n) -> n * 10 end) Enum.take(multiplied, 2) Enum.take( Enum.map( Enum.filter( Enum.slice((1..100), 30, 10), fn(n) -> rem(n, 2) == 0 end ), fn(n) -> n * 10 end ), 2 )
  18. 18. range = (1..100) interval = Enum.slice(range, 30, 10) evens = Enum.filter(interval, fn(n) -> rem(n, 2) == 0 end) multiplied = Enum.map(evens, fn(n) -> n * 10 end) Enum.take(multiplied, 2) (1..100) |> Enum.slice(30, 10) |> Enum.filter(fn(n) -> rem(n, 2) end) |> Enum.map(fn(n) -> n * 10 end) |> Enum.take(2)
  19. 19. (1..100) |> Enum.slice(30, 10) |> Enum.filter(&(rem(&1, 2))) |> Enum.map(&(&1 * 10)) |> Enum.take(2) (1..100) |> Enum.slice(30, 10) |> Enum.filter(fn(n) -> rem(n, 2) end) |> Enum.map(fn(n) -> n * 10 end) |> Enum.take(2)
  20. 20. PIPE OPERATOR |>
  21. 21. function = fn -> IO.puts("Hello from function") end function.() # => Hello from function pid = spawn(function) # => Hello from function Process.alive?(pid) # => false Process.alive?(self) # => true
  22. 22. pid = spawn(fn -> receive do {:say, from} -> send(from, "say what?") _ -> Process.exit(self, :normal) end end) Process.alive?(pid) # => true send(pid, {:say, self}) receive do message -> IO.puts("Recebido: " <> message) end # => Recebido: say what? send(pid, "blabla") Process.alive?(pid) # => false
  23. 23. “PROCESSES” Lightweight Isolated Message passing Mailbox Garbage Collected
  24. 24. pid = spawn(fn -> ... end) Process.exit(pid, :kill)
  25. 25. pid = spawn_link(fn -> receive do {:say, from} -> send(from, "say what?") _ -> Process.exit(self, :normal) end end) send(pid, "blabla") ** (EXIT from #PID<0.47.0>) killed
  26. 26. pid = spawn_link(fn -> ... end) Process.exit(pid, :kill)
  27. 27. pid = spawn_link(fn -> receive do {:say, from} -> send(from, "say what?") _ -> Process.exit(self, :normal) end end) Process.flag(:trap_exit, true) send(pid, "blabla") receive do {:EXIT, _pid, reason} -> IO.puts("Motivo do crash: #{reason}") end # => Motivo do crash: normal
  28. 28. ASYNCHRONOUS EXCEPTIONS (spawn_link, flag, exit)
  29. 29. “GLOBALS” Javascript Scala Go Clojure ...
  30. 30. defmodule Stack do def start_link do Agent.start_link(fn -> [] end, name: __MODULE__) end def pop do Agent.get_and_update(__MODULE__, fn(state) -> [head|tail] = state {head, tail} end) end def push(new_value) do Agent.update(__MODULE__, fn(state) -> [new_value|state] end) end end
  31. 31. Stack.start_link Stack.push "hello" Stack.push "world" Stack.pop # => world Stack.pop # => hello Process.list |> Enum.reverse |> Enum.at(0) |> Process.exit(:kill) Stack.push "foo" # => ** (exit) exited in: GenServer.call(Stack, {:update, ..., 5000) # ** (EXIT) no process # (elixir) lib/gen_server.ex:564: GenServer.call/3
  32. 32. 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
  33. 33. Stack.Supervisor.start_link Stack.push "hello" Stack.push "world" Stack.pop # => world Stack.pop # => hello Process.list |> Enum.reverse |> Enum.at(0) |> Process.exit(:kill) Stack.push "foo" Stack.push "bla" Stack.pop # => bla Stack.pop # => foo
  34. 34. Stack.Supervisor.start_link Process.list |> Enum.reverse |> Enum.at(0) |> Process.exit(:kill)
  35. 35. “YOCTO” SERVICES (micro > nano > pico > femto > atto > zepto > yocto>
  36. 36. APRENDENDO
  37. 37. OBRIGADO! Perguntas? @akitaonrails

×