Slide 1

Slide 1 text

WHY ELIXIR? Elixir Sofia Meetup May 2017

Slide 2

Slide 2 text

WHY ELIXIR? INTRO TO ELIXIR

Slide 3

Slide 3 text

Ventsislav Nikolov @ventsislaf

Slide 4

Slide 4 text

DISCLAIMER NO GIFs

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

WHAT IS ELIXIR?

Slide 7

Slide 7 text

• functional programming language • compiles to erlang bytecode • immutable data • dynamically typed • concurrency, distribution, fault tolerance • hot code swapping

Slide 8

Slide 8 text

DATA TYPES (NOT ALL) 1_000_000 # Integer 314159.0e-5 # Float :apple # Atom "Jon Snow" # String {:error, 404} # Tuple [1, 2, 3] # (Linked) List %{name: "Jon", age: 21} # Map

Slide 9

Slide 9 text

MODULES AND FUNCTIONS

Slide 10

Slide 10 text

ANONYMOUS FUNCTIONS add = fn(a, b) -> a + b end add.(2, 5) # => 7

Slide 11

Slide 11 text

MODULES defmodule Math do def add(a, b) do a + b end defp multiply(a, b) do a * b end end # warning: function multiply/2 is unused Math.add(2, 5) # => 7 Math.multiply(2, 5) # ** (UndefinedFunctionError) function Math.multiply/2 is undefined or private

Slide 12

Slide 12 text

IMMUTABILITY

Slide 13

Slide 13 text

colors = ["green", "apple", "red"] List.delete(colors, "apple") # => ["green", "red"] colors # => ["green", "apple", "red"]

Slide 14

Slide 14 text

PATTERN MATCHING

Slide 15

Slide 15 text

= x = 1 # => 1 1 = x # => 1 2 = x # ** (MatchError) no match of right hand side value: 1 x = 2 # => 2

Slide 16

Slide 16 text

DECOMPOSITION [x, y] = [1, 2] # x => 1, y => 2 [1, x] = [1, 2] # x => 2 [_, x] = [1, 2] # x => 2 [head | tail] = [1, 2, 3] # head => 1, tail => [2, 3] %{name: name} = %{name: "Jon Snow", age: 21} # name => "Jon Snow"

Slide 17

Slide 17 text

RECURSION

Slide 18

Slide 18 text

def sum([]) do 0 end def sum([head | tail]) do head + sum(tail) end

Slide 19

Slide 19 text

PIPE OPERATOR

Slide 20

Slide 20 text

Enum.sort(String.codepoints(String.downcase(word)))

Slide 21

Slide 21 text

lowercase = String.downcase(word) chars = String.codepoints(lowercase) ordered = Enum.sort(chars)

Slide 22

Slide 22 text

word |> String.downcase() |> String.codepoints() |> Enum.sort()

Slide 23

Slide 23 text

Elixir + Erlang = ❤

Slide 24

Slide 24 text

# Erlang rand:uniform(10). # Elixir :rand.uniform(10)

Slide 25

Slide 25 text

CONCURRENCY

Slide 26

Slide 26 text

PROCESSES (not OS) spawn fn -> IO.puts "Hello from another process" end

Slide 27

Slide 27 text

LIGHTWEIGHT • created for couple of microseconds • 1-2 KB initial memory footprint • up to hundreds of millions processes per VM

Slide 28

Slide 28 text

ISOLATION name = "Jon Snow" spawn fn -> IO.puts "Hello, #{name}" end

Slide 29

Slide 29 text

MESSAGES pid = spawn fn -> receive do msg -> IO.puts "received a message: #{msg}" end end send pid, "You know nothing, Jon Snow."

Slide 30

Slide 30 text

msg 1 msg 2 msg 3 mailbox

Slide 31

Slide 31 text

FAULT TOLERANCE

Slide 32

Slide 32 text

:one_for_one :one_for_all

Slide 33

Slide 33 text

DISTRIBUTION

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

$ iex --name one@host $ iex --name two@host $ iex --name three@host # in node one iex> Node.connect :two@host iex> Node.connect :three@host iex> Node.list # => [:two@host, :three@host] # in node two iex> Node.list # => [:one@host, :three@host] iex> Node.spawn :three@host, fn -> IO.puts("hello") end

Slide 36

Slide 36 text

TOOLING

Slide 37

Slide 37 text

IEx (demo) $ iex iex> 2 + 5 7

Slide 38

Slide 38 text

MIX mix new app_name mix compile mix test mix deps.get mix hex.publish

Slide 39

Slide 39 text

OBSERVER :observer.start

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

WHERE TO GO NEXT?

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

THANK YOU!