Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and...

60
Introduction To Elixir By Dan Ivovich October 25, 2017

Transcript of Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and...

Page 1: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Introduction To ElixirBy Dan Ivovich

October 25, 2017

Page 2: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

What is Elixir?→ Started by Jośe Valim around 2012→ Was working on Ruby and Rails→ Wanted better multithreading

→ Invented Elixir→ Compiles to Erlang VM (BEAM) Code

Page 3: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Elixir is...

→ Dynamic→ Functional

Elixir is for...

→ Scalablity→ Maintainablility

Page 4: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Is it Erlang?Leverages the Erlang VM

Page 5: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Why the Erlang VM?

→ Low-latency→ Distributed→ Fault-tolerant

Page 6: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

We want web and stuff!?!?successfully used in

→ web development→ embedded software

Page 7: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Why not just Erlang?Elixir adds...

→ Ruby-inspired syntax→ Great tooling

→ Better code organization facilities

Page 8: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

So what do we get?→ Polymorphism via protocols

→ Shared nothing concurrent programming→ Concurrent programming via message passing

→ Lightweight concurrency→ Meta programming

→ Erlang functions can be called from Elixir→ Lazy and async collections

→ Pattern matching→ Unicode support and UTF-8 strings

→ Support for documentation in the language

Page 9: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Getting Started

Page 10: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Windows❗ ❓ ❗

Sorry !

https://elixir-lang.org/install.html#windows

Looks like it has an installer, I just haven't personally tried it.

Page 11: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Linux/OSX

You can use system packages or Homebrew

I recommend asdf

Page 12: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

https://github.com/asdf-vm/asdf

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.4.0echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bash_profileecho -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bash_profileasdf plugin-add erlangasdf plugin-add elixirasdf install erlang 20.1asdf install elixir 1.5.2

Page 13: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Value TypesIntegers

12345

Page 14: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Value TypesFloats

0.123461.12341222.123123

Page 15: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Value TypesAtoms

:an_atom:another_atom:this_is_not_garbage_collected

Constant, unchangeable, the name is the value

Page 16: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Value TypesRanges

1..5

Basic

Page 17: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Value TypesRegular Expressions

~r/foo/~r/foo/iu> Regex.match?(~r/foo/, "foo")true> Regex.match?(~r/foo/, "bar")false

Pretty normal stuff here

Page 18: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Collection TypesTuples

{:ok, 42, "yes"}{:this, :is, :not, :an, :array}

Fast to access, slow to update or add elements

Page 19: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Collection TypesLinked Lists

list = [1, 2, 3, 4]hd(list) # 1tl(list) # [2, 3, 4]

Fast to prepend, slow to append or count

Page 20: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Collection TypesBinaries

<<1,2>>"Strings have double quotes"

String are UTF-8 encoded binaries

Page 21: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Collection TypesMaps

%{ key => value, another => value2 }countries = %{"USA" => "United States", "Fr" => "France"}countries["USA"] # "United States"

Page 22: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Anonymous FunctionsUnnamed functions

Can be passed as arguments

> add = fn (a, b) -> a + b end#Function<12.99386804/2 in :erl_eval.expr/5>> add.(1, 2)3

Page 23: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Named FunctionsIn a module

defmodule Hello do def say_hello do IO.puts "Hello World" endend

> Hello.say_helloHello World

Page 24: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

StructsStructured Map in a Module

defmodule Client do defstruct first: "Default", last: "Name", phone: nilend

> %Client{}%Client{first: "Default", last: "Name", phone: nil}

> %Client{first: "Awesome"}%Client{first: "Awesome", last: "Name", phone: nil}

> %Client{credit_card: "I Wish"}** (KeyError) key :credit_card not found in: %Client{first: "Default", last: "Name", phone: nil}

Page 25: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pattern MatchingIt isn't assignment

> a = 11> 1 = a1> 2 = a** (MatchError) no match of right hand side value: 1

Page 26: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pattern MatchingIt is binding

And you can rebind

> a = 11> a = 22

Page 27: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pattern MatchingThis is cool

> [1, b, 4] = [1, 5, 4][1, 5, 4]> b5

Page 28: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pattern MatchingIgnore things

> [1, c, _] = [1, 4, 123][1, 4, 123]> c4

Page 29: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pattern MatchingLists

> [head | tail] = [1, 2, 3][1, 2, 3]> head1> tail[2, 3]

Page 30: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pattern MatchingFunction Signatures

defmodule Factorial do def of(0), do: 1 def of(x), do: x * of(x-1)end

Page 31: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pattern MatchingFunction Signatures

def execute({:ok, value}) do IO.puts "Execute: #{value}"end

def execute({:error, reason}) do IO.puts "Error: #{reason}"end

> execute({:ok, "Well Done!"})Execute: Well Done!

> execute({:error, "Fail!"})Error: Fail!

Page 32: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pattern MatchingStructs

case HTTP.get(url) do {:ok, %HTTP.Resp{status: 200, body: body}} -> IO.puts body {:ok, %HTTP.Resp{status: 404}} -> IO.puts "Not found!" {:ok, %HTTP.Resp{status: status}} -> IO.puts "HTTP Status: #{status}" {:error, %HTTP.Error{reason: reason}} -> IO.inspect reason _ -> IO.puts "Some other error"end

Page 33: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Guardsdefmodule Factorial do def of(n) when n == 0, do: 1 def of(n) when n > 0 do n * of(n-1) endend

Page 34: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

ExceptionsUse Pattern Matching Instead

case File.open("no_file.exs") do {:ok, file} -> # do something with file {:error, reason} -> # do something with reasonend

Page 35: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Pipe OperatorChain return value into the first argument of the next function

three = add(1, 2)six = add(three, 3)ten = add(six, 4)

ten = add(add(add(1, 2), 3), 4)

ten = 1 |> add(2) |> add(3) |> add(4)

Page 36: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

More Good StuffMix and IEx

Docs, Tests, and DocTests

Macros

OTP

For another talk

Page 37: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Hello World

Create the file "hello.exs":IO.puts "Hello World"

Run:elixir hello.exs

Output:Hello World

Page 38: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

That was just a script (exs)Elixir can be compiled

defmodule Hello do def say_hello do IO.puts "Hello World" endend

Page 39: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Compiled on they fly

elixir -r hello.ex -e "Hello.say_hello"

Output:Hello World

Page 40: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Compiled into BEAM files for erlang

elixirc -o _build hello.exls _build

Output:Elixir.Hello.beam

There is no reason to do this manually

Page 41: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Comparison! " #

Page 42: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Fibonacci

Page 43: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

FibonacciPython

def fibonacci(n): if n <= 1: return n else: return fibonacci(n-1) + fibonacci(n-2)

Page 44: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

FibonacciRuby

def fibonacci(n) return n if n <= 1 fibonacci(n - 1) + fibonacci(n - 2)end

Page 45: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

FibonacciJavaScript

function fibonacci(num) { if (num <= 1) return num; return fibonacci(num - 1) + fibonacci(num - 2);}

Page 46: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

FibonacciErlang

fibonacci(0) -> 0;fibonacci(1) -> 1;fibonacci(N) -> fibonacci(N - 1) + fibonacci(N - 2).

Page 47: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

FibonacciElixir

defmodule Fibonacci do def fibonacci(n) when n < 2, do: n def fibonacci(n) do fibonacci(n - 1) + fibonacci(n - 2) endend

Page 48: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

ErrorsAll of those examples fail with a negative input

Page 49: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

FibonacciElixir

defmodule Fibonacci do def fibonacci(0), do: 0 def fibonacci(1), do: 1 def fibonacci(n) when n > 1 do fibonacci(n - 1) + fibonacci(n - 2) endend

Page 50: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

FibonacciElixir

> Fibonacci.fibonacci(-1)** (FunctionClauseError) no function clause matching in Fibonacci.fibonacci/1

The following arguments were given to Fibonacci.fibonacci/1:

# 1 -1

fib.exs:2: Fibonacci.fibonacci/1 fib.exs:9: (file) (elixir) lib/code.ex:376: Code.require_file/2

Page 51: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

FibonacciElixir

defmodule Fibonacci do def fibonacci(n) when n < 0, do: {:error, "No negative numbers!"} def fibonacci(0), do: {:ok, 0} def fibonacci(1), do: {:ok, 1} def fibonacci(n) when n > 1 do {:ok, left} = fibonacci(n - 1) {:ok, right} = fibonacci(n - 2) {:ok, left + right} endend

Page 52: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

IO.inspect Fibonacci.fibonacci(-1)IO.inspect Fibonacci.fibonacci(2)IO.inspect Fibonacci.fibonacci(3)IO.inspect Fibonacci.fibonacci(4)IO.inspect Fibonacci.fibonacci(5)IO.inspect Fibonacci.fibonacci(6)IO.inspect Fibonacci.fibonacci(7)

$ elixir fib.exs{:error, "No negative numbers!"}{:ok, 1}{:ok, 2}{:ok, 3}{:ok, 5}{:ok, 8}{:ok, 13}

Page 53: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

What is cool about all that?→ Fault-tolerance is fundamental to the language→ language constructs make it easy to be explicit

→ explicitly describe how data should flow→ then data flows

Page 54: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Stuff like this

def find_for_authorization do query = # beyond the scope of this presentation case Repo.one(query) do %User{} = user -> {:ok, user} nil -> {:error, :unauthorized} endend

def login do case find_for_authorization do {:ok, user} -> # create session {:error, :unauthorized} -> # return a 403 endend

Page 55: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Really cool when paired with OTPCome to another meetup for more on that!

Page 56: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Resources - Books

Programming Elixirhttps://pragprog.com/book/elixir13/programming-elixir-1-3

Programming Phoenixhttps://pragprog.com/book/phoenix/programming-phoenix

Metaprogramming Elixirhttps://pragprog.com/book/cmelixir/metaprogramming-elixir

Functional Web Development with Elixir, OTP, and Phoenixhttps://pragprog.com/book/lhelph/functional-web-development-with-elixir-

otp-and-phoenix

Page 57: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Resources - Newsletters

Elixir Radarhttp://plataformatec.com.br/elixir-radar

Elixir Weeklyhttps://elixirweekly.net/

Elixir Digesthttps://elixirdigest.net

Page 58: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Resources - ConferencesElixir With love (Nov, Providence)https://www.elixir-with-love.com/

ElixirConfMX (Nov, Mexico City)http://elixirconf.mx/

Lonestar ElixirConf (Feb, Austin)http://lonestarelixir.com/2018

ElixirDaze (March, Denver)http://www.elixirdaze.com/

ElixirConf EU (April, Warsaw Poland)http://www.elixirconf.eu/

ElixirConf (Sept, Bellevue)https://elixirconf.com/

Page 59: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Resources - Others

Elixir Fountain Podcasthttps://twitter.com/elixirfountain

Elixir Status Twitterhttps://twitter.com/elixirstatus

Elixir Slackhttp://elixir-slackin.herokuapp.com/

Baltimore Meetuphttps://www.meetup.com/Baltimore-Elixir-and-Erlang-Meetup/

Page 60: Introduction To Elixir · → Low-latency → Distributed → Fault-tolerant. We want web and stuff!?!? successfully used in → web development → embedded software. Why not just

Questions?❓