Slide 1

Slide 1 text

Programação Funcional & Elixir

Slide 2

Slide 2 text

@amandasposito @amsposito @amandasposito

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Falando um pouco de história

Slide 5

Slide 5 text

http://www.gotw.ca/publications/concurrency-ddj.htm

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

“Efficiency and performance optimization will get more, not less, important.”

Slide 9

Slide 9 text

“Applications will increasingly need to be concurrent if they want to fully exploit continuing exponential CPU throughput gains.”

Slide 10

Slide 10 text

Por que isso afeta nossa aplicação?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Elixir criada por José Valim

Slide 13

Slide 13 text

Percebeu que as ferramentas que existiam não eram boas para concorrência

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Elixir roda em cima da maquina virtual do Erlang

Slide 16

Slide 16 text

Erlang é conhecido por rodar aplicações com latência baixa, distribuídas ou tolerante a falhas

Slide 17

Slide 17 text

Escalabilidade Tolerância a falhas Compatível com Erlang Hot Code Swap Linguagem Dinâmica Metaprogramação Polimorfismo Concorrência

Slide 18

Slide 18 text

Diferentes paradigmas

Slide 19

Slide 19 text

Paradigma Funcional

Slide 20

Slide 20 text

–Introduction to Functional Programming - Richard Bird Philip Wadler “Programming in a functional language consists of building definitions and using the computer to evaluate expressions.”

Slide 21

Slide 21 text

Imutabilidade

Slide 22

Slide 22 text

Consistência de dados

Slide 23

Slide 23 text

Concorrência Capacidade de lidar com várias coisas ao mesmo tempo

Slide 24

Slide 24 text

https://blog.whatsapp.com/196/1-million-is-so-2011?

Slide 25

Slide 25 text

Vamos resolver um problema

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

switch = fn (bulb) -> ... end switch.(bulb)

Slide 29

Slide 29 text

defmodule LightBulb do def switch(human, bulb) do end end LightBulb.switch(bulb)

Slide 30

Slide 30 text

Pattern Matching

Slide 31

Slide 31 text

name = "Amanda"

Slide 32

Slide 32 text

Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> name = "Amanda" "Amanda" iex(2)> "Amanda" = name "Amanda" iex(3)> "Amanda S" = name ** (MatchError) no match of right hand side value: "Amanda"

Slide 33

Slide 33 text

defmodule LightBulb do def switch(human = %Human{}, bulb = %Bulb{}) … end end

Slide 34

Slide 34 text

Function Arity

Slide 35

Slide 35 text

defmodule LightBulb do def switch(human, bulb) do ... end def switch(_) do IO.puts "Precisamos de um humano e de uma escada." end end

Slide 36

Slide 36 text

Guards

Slide 37

Slide 37 text

defmodule LightBulb do def switch(human, bulb) when bulb.burned_out == true do … end end

Slide 38

Slide 38 text

If and unless

Slide 39

Slide 39 text

defmodule LightBulb do def switch(human, bulb) when bulb.burned_out == true do ladder = Ladder.get_average_height() if ladder.under do … end end end

Slide 40

Slide 40 text

Pipe Operator

Slide 41

Slide 41 text

new_bulb = Bulb.get_bulb() lader = Ladder.get_average_height() human = Human.climb(human, ladder) human = Human.remove_bulb(human, bulb) human = Human.put_bulb(human, new_bulb)

Slide 42

Slide 42 text

Ladder.get_average_height() |> Human.climb(human) |> Human.remove_bulb(bulb) |> Human.put_bulb(new_bulb)

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Recursão

Slide 45

Slide 45 text

numbers = 3; for(i = numbers; i > 0; i--) { console.log(i); }

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

defmodule NaturalNums do # Uma clausula para parar a recursão def print(1), do: IO.puts(1) def print(n) do # Imprime o número" IO.puts(n) # Chama a si mesmo com um valor a menos print(n - 1) end end

Slide 48

Slide 48 text

iex(1)> NaturalNums.print(3) 3 2 1 :ok

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

https://www.codeschool.com/courses/try-elixir

Slide 52

Slide 52 text

https://www.dailydrip.com/topics/elixir https://github.com/dailydrip/firestorm

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Obrigada!

Slide 56

Slide 56 text

Referências • https://hipsters.tech/elixir-a-linguagem-hipster- hipsters-48/ • https://codewords.recurse.com/issues/one/an- introduction-to-functional-programming • http://theerlangelist.blogspot.com.br/2013/05/working- with-immutable-data.html