SlideShare una empresa de Scribd logo
1 de 57
Descargar para leer sin conexión
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(String.t)
def say(name) when is_string(name) do
IO.puts "Hello " <> name
end
end
Teste.say "Fabio"
OPTIONAL PARENTHESIS
-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
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
CALL BY PATTERN
{: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: ...
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 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}]
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]
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
)
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)
(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)
PIPE OPERATOR
|>
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
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
“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(pid, "blabla")
** (EXIT from #PID<0.47.0>) killed
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)
Process.flag(:trap_exit, true)
send(pid, "blabla")
receive do
{:EXIT, _pid, reason} ->
IO.puts("Motivo do crash: #{reason}")
end
# => Motivo do crash: normal
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(__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
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
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
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
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

Más contenido relacionado

La actualidad más candente

Part2 from math import * from simpson import * k=1 def f(x): return (exp(-(x...
Part2 from math import * from simpson import *  k=1 def f(x): return (exp(-(x...Part2 from math import * from simpson import *  k=1 def f(x): return (exp(-(x...
Part2 from math import * from simpson import * k=1 def f(x): return (exp(-(x...
hwbloom25
 

La actualidad más candente (20)

Let's golang
Let's golangLet's golang
Let's golang
 
PubNative Tracker
PubNative TrackerPubNative Tracker
PubNative Tracker
 
FPBrno 2018-05-22: Benchmarking in elixir
FPBrno 2018-05-22: Benchmarking in elixirFPBrno 2018-05-22: Benchmarking in elixir
FPBrno 2018-05-22: Benchmarking in elixir
 
Python Tidbits
Python TidbitsPython Tidbits
Python Tidbits
 
ECMAScript 6 new features
ECMAScript 6 new featuresECMAScript 6 new features
ECMAScript 6 new features
 
Build a compiler in 2hrs - NCrafts Paris 2015
Build a compiler in 2hrs -  NCrafts Paris 2015Build a compiler in 2hrs -  NCrafts Paris 2015
Build a compiler in 2hrs - NCrafts Paris 2015
 
Part2 from math import * from simpson import * k=1 def f(x): return (exp(-(x...
Part2 from math import * from simpson import *  k=1 def f(x): return (exp(-(x...Part2 from math import * from simpson import *  k=1 def f(x): return (exp(-(x...
Part2 from math import * from simpson import * k=1 def f(x): return (exp(-(x...
 
Javascript - The basics
Javascript - The basicsJavascript - The basics
Javascript - The basics
 
[EN] Ada Lovelace Day 2014 - Tampon run
[EN] Ada Lovelace Day 2014  - Tampon run[EN] Ada Lovelace Day 2014  - Tampon run
[EN] Ada Lovelace Day 2014 - Tampon run
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my day
 
Introduction to jRuby
Introduction to jRubyIntroduction to jRuby
Introduction to jRuby
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
The Lesser Known Features of ECMAScript 6
The Lesser Known Features of ECMAScript 6The Lesser Known Features of ECMAScript 6
The Lesser Known Features of ECMAScript 6
 
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
 
Perl 5.10 on OSDC.tw 2009
Perl 5.10 on OSDC.tw 2009Perl 5.10 on OSDC.tw 2009
Perl 5.10 on OSDC.tw 2009
 
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
 
Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)Hangman Game Programming in C (coding)
Hangman Game Programming in C (coding)
 
大量地区化解决方案V5
大量地区化解决方案V5大量地区化解决方案V5
大量地区化解决方案V5
 
Go a crash course
Go   a crash courseGo   a crash course
Go a crash course
 
The groovy puzzlers (as Presented at Gr8Conf US 2014)
The groovy puzzlers (as Presented at Gr8Conf US 2014)The groovy puzzlers (as Presented at Gr8Conf US 2014)
The groovy puzzlers (as Presented at Gr8Conf US 2014)
 

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

Similar a Elixir - Tolerância a Falhas para Adultos - Secot VIII Sorocaba (20)

Elixir -Tolerância a Falhas para Adultos - GDG Campinas
Elixir  -Tolerância a Falhas para Adultos - GDG CampinasElixir  -Tolerância a Falhas para Adultos - GDG Campinas
Elixir -Tolerância a Falhas para Adultos - GDG Campinas
 
Lập trình Python cơ bản
Lập trình Python cơ bảnLập trình Python cơ bản
Lập trình Python cơ bản
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Java VS Python
Java VS PythonJava VS Python
Java VS Python
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Basics
BasicsBasics
Basics
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap course
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Elixir cheatsheet
Elixir cheatsheetElixir cheatsheet
Elixir cheatsheet
 
Ruby - Uma Introdução
Ruby - Uma IntroduçãoRuby - Uma Introdução
Ruby - Uma Introdução
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdf
 
FS2 for Fun and Profit
FS2 for Fun and ProfitFS2 for Fun and Profit
FS2 for Fun and Profit
 
Super Advanced Python –act1
Super Advanced Python –act1Super Advanced Python –act1
Super Advanced Python –act1
 

Más de Fabio Akita

Más de Fabio Akita (20)

Devconf 2019 - São Carlos
Devconf 2019 - São CarlosDevconf 2019 - São Carlos
Devconf 2019 - São Carlos
 
Meetup Nerdzão - English Talk about Languages
Meetup Nerdzão  - English Talk about LanguagesMeetup Nerdzão  - English Talk about Languages
Meetup Nerdzão - English Talk about Languages
 
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
Desmistificando Blockchains p/ Developers - Criciuma Dev Conf 2018
 
Desmistificando Blockchains - 20o Encontro Locaweb SP
Desmistificando Blockchains - 20o Encontro Locaweb SPDesmistificando Blockchains - 20o Encontro Locaweb SP
Desmistificando Blockchains - 20o Encontro Locaweb SP
 
Desmistificando Blockchains - Insiter Goiania
Desmistificando Blockchains - Insiter GoianiaDesmistificando Blockchains - Insiter Goiania
Desmistificando Blockchains - Insiter Goiania
 
Blockchain em 7 minutos - 7Masters
Blockchain em 7 minutos - 7MastersBlockchain em 7 minutos - 7Masters
Blockchain em 7 minutos - 7Masters
 
Desmistificando Mitos de Tech Startups - Intercon 2017
Desmistificando Mitos de Tech Startups - Intercon 2017Desmistificando Mitos de Tech Startups - Intercon 2017
Desmistificando Mitos de Tech Startups - Intercon 2017
 
30 Days to Elixir and Crystal and Back to Ruby
30 Days to Elixir and Crystal and Back to Ruby30 Days to Elixir and Crystal and Back to Ruby
30 Days to Elixir and Crystal and Back to Ruby
 
Uma Discussão sobre a Carreira de TI
Uma Discussão sobre a Carreira de TIUma Discussão sobre a Carreira de TI
Uma Discussão sobre a Carreira de TI
 
THE CONF - Opening Keynote
THE CONF - Opening KeynoteTHE CONF - Opening Keynote
THE CONF - Opening Keynote
 
A Journey through New Languages - Rancho Dev 2017
A Journey through New Languages - Rancho Dev 2017A Journey through New Languages - Rancho Dev 2017
A Journey through New Languages - Rancho Dev 2017
 
Desmistificando Mitos de Startups - Sebrae - AP
Desmistificando Mitos de Startups - Sebrae - APDesmistificando Mitos de Startups - Sebrae - AP
Desmistificando Mitos de Startups - Sebrae - AP
 
A Journey through New Languages - Guru Sorocaba 2017
A Journey through New Languages - Guru Sorocaba 2017A Journey through New Languages - Guru Sorocaba 2017
A Journey through New Languages - Guru Sorocaba 2017
 
A Journey through New Languages - Insiter 2017
A Journey through New Languages - Insiter 2017A Journey through New Languages - Insiter 2017
A Journey through New Languages - Insiter 2017
 
A Journey through New Languages - Locaweb Tech Day
A Journey through New Languages - Locaweb Tech DayA Journey through New Languages - Locaweb Tech Day
A Journey through New Languages - Locaweb Tech Day
 
A Journey through new Languages - Intercon 2016
A Journey through new Languages - Intercon 2016A Journey through new Languages - Intercon 2016
A Journey through new Languages - Intercon 2016
 
Premature Optimization 2.0 - Intercon 2016
Premature Optimization 2.0 - Intercon 2016Premature Optimization 2.0 - Intercon 2016
Premature Optimization 2.0 - Intercon 2016
 
Conexão Kinghost - Otimização Prematura
Conexão Kinghost - Otimização PrematuraConexão Kinghost - Otimização Prematura
Conexão Kinghost - Otimização Prematura
 
The Open Commerce Conference - Premature Optimisation: The Root of All Evil
The Open Commerce Conference - Premature Optimisation: The Root of All EvilThe Open Commerce Conference - Premature Optimisation: The Root of All Evil
The Open Commerce Conference - Premature Optimisation: The Root of All Evil
 
Premature optimisation: The Root of All Evil
Premature optimisation: The Root of All EvilPremature optimisation: The Root of All Evil
Premature optimisation: The Root of All Evil
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

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

  • 1. ELIXIRTolerância a Falhas para Adultos Fabio Akita
  • 3. Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
  • 4. Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
  • 5. Rubyconf Brasil 2016 – 23 e 24 de SetembroRubyconf Brasil 2016 – 23 e 24 de Setembro
  • 6.
  • 7. Chris McCordChris McCord José ValimJosé Valim
  • 8. Joe Armstrong - EricssonJoe Armstrong - Ericsson
  • 9.
  • 10.
  • 11. 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"
  • 13. -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
  • 14. 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
  • 16. {: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: ...
  • 18. 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}]
  • 19. 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]
  • 20. 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 )
  • 21. 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)
  • 22. (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)
  • 24. 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
  • 25. 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
  • 27. pid = spawn(fn -> ... end) Process.exit(pid, :kill)
  • 28. 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
  • 29. pid = spawn_link(fn -> ... end) Process.exit(pid, :kill)
  • 30. 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
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. Stack.Supervisor.start_link Process.list |> Enum.reverse |> Enum.at(0) |> Process.exit(:kill)
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46. “YOCTO” SERVICES (micro > nano > pico > femto > atto > zepto > yocto>
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 53.
  • 54.
  • 55.
  • 56.