Slide 1

Slide 1 text

@georgeguimaraes / @plataformatec ELIXIR. PROGRAMAÇÃO FUNCIONAL E PRAGMÁTICA

Slide 2

Slide 2 text

GEORGE GUIMARÃES

Slide 3

Slide 3 text

consulting and software engineering

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

NOSSA GERAÇÃO TEM UM PROBLEMA. Desenvolvedores web tem que lidar com concorrência. Não há escapatória.

Slide 7

Slide 7 text

CONCORRÊNCIA. Capacidade de lidar com várias coisas (ao mesmo tempo, ou serialmente).

Slide 8

Slide 8 text

PARALELISMO. Capacidade de fazer várias coisas ao mesmo tempo.

Slide 9

Slide 9 text

CONCORRÊNCIA. Websockets, HTTP2, Alta quantidade de requests, demanda instável.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

THREADS E EVENT LOOP. Modelos primitivos para lidar com concorrência.

Slide 13

Slide 13 text

“what makes multithreaded programming difficult is not that writing it is hard, but that testing it is hard. It’s not the pitfalls that you can fall into; it’s the fact that you don’t necessarily know whether you’ve fallen into one of them. ”

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

— ROBERT VIRDING “Any sufficiently complicated concurrent program in another language contains an ad hoc informally-specified bug-ridden slow implementation of half of Erlang.”

Slide 16

Slide 16 text

PLATAFORMA ELIXIR. Erlang and OTP, now with modern tooling.

Slide 17

Slide 17 text

30 anos

Slide 18

Slide 18 text

Switch Switch

Slide 19

Slide 19 text

Switch Browser Endpoint Server

Slide 20

Slide 20 text

— ELIXIR DEVELOPER “We stand in the shoulders of giants”

Slide 21

Slide 21 text

DOCUMENTAÇÃO DE ALTO NÍVEL. Documentação ruim é bug.

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

FERRAMENTAL EMBUTIDO. Hex, Mix, ExUnit.

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

LINGUAGEM FUNCIONAL. Não temos classes nem objetos.

Slide 27

Slide 27 text

IMUTABILIDADE. “Isso muda tudo”

Slide 28

Slide 28 text

NÃO TENHO LOOPS!

Slide 29

Slide 29 text

defmodule Fibonacci do def calc(0), do: 0 def calc(1), do: 1 def calc(n), do: calc(n-1) + calc(n-2) end Fibonacci.calc(10) # => 55

Slide 30

Slide 30 text

MAS, E COMO FAZER UM CONTADOR? Não é possível mudar o conteúdo de uma variável????

Slide 31

Slide 31 text

defmodule Counter do def start(value) do receive do :increment -> start(value + 1) {:get, pid} -> send(pid, value) end end end

Slide 32

Slide 32 text

defmodule Counter do def start(value) do receive do :increment -> start(value + 1) {:get, caller} -> send(caller, value) end end end pid = spawn(fn -> Counter.start(10) end) send(pid, :increment) send(pid, :increment) send(pid, :increment) send(pid, {:get, self}) flush # => 13

Slide 33

Slide 33 text

shell

Slide 34

Slide 34 text

shell Counter.start(10) spawn

Slide 35

Slide 35 text

shell 11 increment

Slide 36

Slide 36 text

shell 12 increment

Slide 37

Slide 37 text

shell 13 increment

Slide 38

Slide 38 text

shell 13 :get, self 13

Slide 39

Slide 39 text

defmodule Counter do def start(value) do receive do :increment -> start(value + 1) {:get, caller} -> send(caller, value) end end end pid = spawn(fn -> Counter.start(10) end) send(pid, :increment) send(pid, :increment) send(pid, :increment) send(pid, {:get, self}) flush # => 13

Slide 40

Slide 40 text

ACTOR MODEL. 1. Enviar mensagens para outros atores; 2. Criar novos atores; 3. Especificar o comportamento para as próximas mensagens.

Slide 41

Slide 41 text

Sequential code

Slide 42

Slide 42 text

Sequential code elixir

Slide 43

Slide 43 text

elixir

Slide 44

Slide 44 text

elixir

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

FAULT-TOLERANT. DISTRIBUTED. O objetivo não era concorrência.

Slide 47

Slide 47 text

Switch Switch

Slide 48

Slide 48 text

CONCORRÊNCIA É UM CASO DE DISTRIBUIÇÃO. Tá distribuído, mas apenas em uma máquina.

Slide 49

Slide 49 text

elixir

Slide 50

Slide 50 text

defmodule MyApp do use Application def start(_type, _args) do import Supervisor.Spec, warn: false children = [ supervisor(Playfair.Repo, []), worker(Playfair.Mailer, []), worker(Playfair.FileWriter, []), ] opts = [strategy: :one_for_one, name: Playfair.Supervisor] Supervisor.start_link(children, opts) end end

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

ERROS ACONTECEM. Let it crash.

Slide 53

Slide 53 text

MAS SERÁ QUE FUNCIONA MESMO?

Slide 54

Slide 54 text

http://blog.whatsapp.com/index.php/ 2012/01/1-million-is-so-2011/ 2 million connections on a single node

Slide 55

Slide 55 text

Intel Xeon CPU X5675 @ 3.07GHz 24 CPU - 96GB Using 40% of CPU and Memory

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

USO EFICIENTE!. Usa todos os cores da máquina, não só pra processar requests web, mas até pra rodar testes

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

MICROSERVIÇOS EM UM MUNDO ELIXIR. Precisamos sempre de APIs HTTP?

Slide 64

Slide 64 text

elixir

Slide 65

Slide 65 text

node@srv2 node@srv1 elixir

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

OUTRAS VANTAGENS. • Garbage Collector por processo • Latência previsível • Separação de dados e comportamento (FP)

Slide 68

Slide 68 text

ONDE APRENDER?.

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

http://plataformatec.com.br/elixir-radar

Slide 72

Slide 72 text

https://elixirforum.com

Slide 73

Slide 73 text

OBRIGADO!! 
 @plataformatec
 @georgeguimaraes

Slide 74

Slide 74 text

@elixirlang / elixir-lang.org